Skip to content

Commit

Permalink
chore: add website-demo.bats to generate json variables and response …
Browse files Browse the repository at this point in the history
…files (#172)

* chore: add website-demo.bats to generate json variables and response files

* chore: add gql files to website/static

* chrore: add variables and response json files with script to import

* chore: update the mdx files to use the gql and json files

* chore: add website demo jobs to github action on publish

* chore: test gh action

* chore: finalize github action to update website demo files

* chore: save the gql files to website/static in the bats script
  • Loading branch information
openoms authored Jul 22, 2024
1 parent 4b80d3d commit e1b75d0
Show file tree
Hide file tree
Showing 34 changed files with 671 additions and 429 deletions.
11 changes: 7 additions & 4 deletions .github/workflows/website-update-api-reference.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:
workflow_dispatch:

jobs:
update-api-reference:
update-api-reference-and-website-demo-files:
runs-on: ubuntu-latest
steps:
- uses: DeterminateSystems/nix-installer-action@v12
Expand All @@ -20,12 +20,15 @@ jobs:
npm install spectaql
npx spectaql spectaql-config.yml -t static -f api-reference.html
- name: Deploy the API reference
- name: Generate the website demo files
run: nix develop -c make e2e

- name: Commit the API reference and website demo changes
run: |
git config --local user.name 'github-actions[bot]'
git config --local user.email 'github-actions[bot]@users.noreply.github.com'
git add website/static/api-reference.html
git commit -m "docs: api reference update: $GITHUB_SHA"
git add website/static
git commit -m "docs: api reference and website examples update: $GITHUB_SHA"
git push origin HEAD:main
- name: Install Website Dependencies
Expand Down
174 changes: 174 additions & 0 deletions bats/website-demo.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
#!/usr/bin/env bats

load "helpers"

setup_file() {
start_server
}

teardown_file() {
stop_server
}

save_json() {
local filename="$1"
local json_content="$2"
if [[ -z "$json_content" || "$json_content" == "null" ]]; then
echo "JSON content is empty or null, exiting."
exit 1
fi
echo "$json_content" | jq . >"$filename" || {
echo "Failed to write JSON to $filename"
exit 1
}
}

@test "cala: generate variables and responses for the website demo" {
# creating a journal
journal_id=$(random_uuid)
variables=$(jq -n --arg journal_id "$journal_id" '{
"input": {
"journalId": $journal_id,
"name": "General Ledger"
}
}')
gql_request='journal-create'
exec_graphql "$gql_request" "$variables"
[[ "$output" != "null" ]] || exit 1
save_json "${REPO_ROOT}/website/static/gql/variables/journalCreate.json" "$variables"
save_json "${REPO_ROOT}/website/static/gql/responses/journalCreateResponse.json" "$output"
cp "${REPO_ROOT}/bats/gql/${gql_request}.gql" "${REPO_ROOT}/website/static/gql/"

# create liability account
liability_account_id=$(random_uuid)
variables=$(jq -n --arg liability_account_id "$liability_account_id" '{
"input": {
"accountId": $liability_account_id,
"name": "Alice - Checking",
"code": ("ALICE.CHECKING-" + $liability_account_id),
"normalBalanceType": "CREDIT"
}
}')
gql_request='account-create'
exec_graphql "$gql_request" "$variables"
[[ "$output" != "null" ]] || exit 1
save_json "${REPO_ROOT}/website/static/gql/variables/accountCreateChecking.json" "$variables"
save_json "${REPO_ROOT}/website/static/gql/responses/accountCreateCheckingResponse.json" "$output"
cp "${REPO_ROOT}/bats/gql/${gql_request}.gql" "${REPO_ROOT}/website/static/gql/"

# create asset account
asset_account_id=$(random_uuid)
variables=$(jq -n --arg asset_account_id "$asset_account_id" '{
"input": {
"accountId": $asset_account_id,
"name": "Assets",
"code": ("ASSET-"+ $asset_account_id),
"normalBalanceType": "DEBIT"
}
}')
exec_graphql "$gql_request" "$variables"
[[ "$output" != "null" ]] || exit 1
save_json "${REPO_ROOT}/website/static/gql/variables/accountCreateDebit.json" "$variables"
save_json "${REPO_ROOT}/website/static/gql/responses/accountCreateDebitResponse.json" "$output"
cp "${REPO_ROOT}/bats/gql/${gql_request}.gql" "${REPO_ROOT}/website/static/gql/"

# create transaction templates
deposit_template_id=$(random_uuid)
withdrawal_template_id=$(random_uuid)
variables=$(jq -n --arg depositTemplateId "$deposit_template_id" --arg withdrawalTemplateId "$withdrawal_template_id" --arg assetAccountId "$asset_account_id" --arg journalId "$journal_id" '{
"depositTemplateId": $depositTemplateId,
"depositTemplateCode": ("DEPOSIT-" + $depositTemplateId),
"withdrawalTemplateId": $withdrawalTemplateId,
"withdrawalTemplateCode": ("WITHDRAWAL-" + $withdrawalTemplateId),
"assetAccountId": ("uuid(\u0027" + $assetAccountId + "\u0027)"),
"journalId": ("uuid(\u0027" + $journalId + "\u0027)")
}')
gql_request='tx-template-create'
exec_graphql "$gql_request" "$variables"
save_json "${REPO_ROOT}/website/static/gql/variables/txTemplateCreate.json" "$variables"
save_json "${REPO_ROOT}/website/static/gql/responses/txTemplateCreateResponse.json" "$output"
cp "${REPO_ROOT}/bats/gql/${gql_request}.gql" "${REPO_ROOT}/website/static/gql/"

# post transaction
transaction_id=$(random_uuid)
variables=$(jq -n --arg transaction_id "$transaction_id" --arg account_id "$liability_account_id" --arg depositTemplateId "$deposit_template_id" '{
"input": {
"transactionId": $transaction_id,
"txTemplateCode": ("DEPOSIT-" + $depositTemplateId),
"params": {
"account": $account_id,
"amount": "9.53",
"effective": "2022-09-21"
}
}
}')
gql_request='transaction-post'
exec_graphql "$gql_request" "$variables"
correlation_id=$(graphql_output '.data.transactionPost.transaction.correlationId')
[[ $correlation_id == $transaction_id ]] || exit 1
save_json "${REPO_ROOT}/website/static/gql/variables/transactionPost.json" "$variables"
save_json "${REPO_ROOT}/website/static/gql/responses/transactionPostResponse.json" "$output"
cp "${REPO_ROOT}/bats/gql/${gql_request}.gql" "${REPO_ROOT}/website/static/gql/"

# check account balance
variables=$(jq -n --arg journalId "$journal_id" --arg accountId "$liability_account_id" '{
"accountId": $accountId,
"journalId": $journalId,
"currency": "USD"
}')
gql_request='account-with-balance'
exec_graphql "$gql_request" "$variables"
balance=$(graphql_output '.data.account.balance.settled.normalBalance.units')
[[ $balance == "9.53" ]] || exit 1
save_json "${REPO_ROOT}/website/static/gql/variables/accountWithBalance.json" "$variables"
save_json "${REPO_ROOT}/website/static/gql/responses/accountWithBalanceResponse.json" "$output"
cp "${REPO_ROOT}/bats/gql/${gql_request}.gql" "${REPO_ROOT}/website/static/gql/"

# create account set
account_set_id=$(random_uuid)
variables=$(jq -n --arg account_set_id "$account_set_id" --arg journal_id "$journal_id" '{
"input": {
"accountSetId": $account_set_id,
"journalId": $journal_id,
"name": "Account Set",
"normalBalanceType": "CREDIT"
}
}')
gql_request='account-set-create'
exec_graphql "$gql_request" "$variables"
res=$(graphql_output '.data.accountSetCreate.accountSet.accountSetId')
[[ "$res" != "null" ]] || exit 1
save_json "${REPO_ROOT}/website/static/gql/variables/accountSetCreate.json" "$variables"
save_json "${REPO_ROOT}/website/static/gql/responses/accountSetCreateResponse.json" "$output"
cp "${REPO_ROOT}/bats/gql/${gql_request}.gql" "${REPO_ROOT}/website/static/gql/"

# add account to account set
variables=$(jq -n --arg account_set_id "$account_set_id" --arg member_id "$liability_account_id" '{
"input": {
"accountSetId": $account_set_id,
"memberId": $member_id,
"memberType": "ACCOUNT"
}
}')
gql_request='add-to-account-set'
exec_graphql "$gql_request" "$variables"
balance=$(graphql_output '.data.addToAccountSet.accountSet.balance.settled.normalBalance.units')
[[ $balance == "9.53" ]] || exit 1
save_json "${REPO_ROOT}/website/static/gql/variables/addToAccountSet.json" "$variables"
save_json "${REPO_ROOT}/website/static/gql/responses/addToAccountSetResponse.json" "$output"
cp "${REPO_ROOT}/bats/gql/${gql_request}.gql" "${REPO_ROOT}/website/static/gql/"

# balance check for account set
variables=$(jq -n --arg journalId "$journal_id" --arg accountSetId "$account_set_id" '{
"accountSetId": $accountSetId,
"journalId": $journalId,
"currency": "USD"
}')
gql_request='account-set-with-balance'
exec_graphql "$gql_request" "$variables"
balance=$(graphql_output '.data.accountSet.balance.settled.normalBalance.units')
[[ $balance == "9.53" ]] || exit 1
save_json "${REPO_ROOT}/website/static/gql/variables/accountSetWithBalance.json" "$variables"
save_json "${REPO_ROOT}/website/static/gql/responses/accountSetWithBalanceResponse.json" "$output"
cp "${REPO_ROOT}/bats/gql/${gql_request}.gql" "${REPO_ROOT}/website/static/gql/"
}
135 changes: 17 additions & 118 deletions website/docs/demo/account-set.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ title: Use an Account Set
slug: /docs/account-set
---

import {
GraphQLBody,
GraphQLVariables,
GraphQLResponse,
} from "../../src/components/GraphQLDisplay";

Group the created accounts into an account set and check its balance.

## Create an Account Set
Expand All @@ -17,46 +23,17 @@ This initial step sets up the framework for grouping accounts under a common set
- **Name**: The `name` parameter specifies the label or designation of the account set. This name helps users and systems identify and refer to the account set in operations and reports.
- **Normal Balance Type**: The `normalBalanceType` indicates the expected normal balance of the account set (either 'DEBIT' or 'CREDIT'). This setting is foundational for ensuring that the account set correctly represents the nature of the majority of its transactions, aiding in proper financial analysis.

```json
{
"input": {
"accountSetId": "e0cacaef-b692-48d6-81e2-238a5a614a04",
"journalId": "bcc24f47-990c-457d-88cb-76332450ac77",
"name": "Main Account Set",
"normalBalanceType": "CREDIT"
}
}
```
<GraphQLVariables variablesPath="/gql/variables/accountSetCreate.json" />

### GraphQL Request Body

```graphql
mutation accountSetCreate($input: AccountSetCreateInput!) {
accountSetCreate(input: $input) {
accountSet {
accountSetId
name
}
}
}
```
<GraphQLBody queryPath="/gql/account-set-create.gql" />

### Response

```json
{
"data": {
"accountSetCreate": {
"accountSet": {
"accountSetId": "e0cacaef-b692-48d6-81e2-238a5a614a04",
"name": "Main Account Set"
}
}
}
}
```

## Add Accounts to the Account Set
<GraphQLResponse responsePath="/gql/responses/accountSetCreateResponse.json" />

## Add an Account to the Account Set

After creating an account set, this section explains how to add individual accounts to it.

Expand All @@ -66,57 +43,15 @@ After creating an account set, this section explains how to add individual accou
- **Member ID**: The `memberId` refers to the unique identifier of the account (e.g., `Alice - Checking`) being added to the account set.
- **Member Type**: The `memberType` indicates the type of member being added to the set, in this case, an "ACCOUNT." This helps the system understand how to treat the member within the set, whether it's an individual account or another entity type.

```json
{
"input": {
"accountSetId": "e0cacaef-b692-48d6-81e2-238a5a614a04",
"memberId": "3a7d421b-7f5a-43ca-ba6f-5f3e6ee67237",
"memberType": "ACCOUNT"
}
}
```
<GraphQLVariables variablesPath="/gql/variables/addToAccountSet.json" />

### GraphQL Request Body

```graphql
mutation addToAccountSet($input: AddToAccountSetInput!) {
addToAccountSet(input: $input) {
accountSet {
accountSetId
name
balance(currency: "USD") {
settled {
normalBalance {
units
}
}
}
}
}
}
```
<GraphQLBody queryPath="/gql/add-to-account-set.gql" />

### Response

```json
{
"data": {
"addToAccountSet": {
"accountSet": {
"accountSetId": "e0cacaef-b692-48d6-81e2-238a5a614a04",
"name": "Main Account Set",
"balance": {
"settled": {
"normalBalance": {
"units": "9.53"
}
}
}
}
}
}
}
```
<GraphQLResponse responsePath="/gql/responses/addToAccountSetResponse.json" />

## Check the Balance of the Account Set

Expand All @@ -128,51 +63,15 @@ This section explains how to query the balance of the entire account set. This o
- **Journal ID**: The `journalId` helps to specify the journal context for the balance query. Since an account set could potentially be linked to multiple journals over time, this helps ensure the balance retrieved is relevant to the specified journal.
- **Currency**: The `currency` specifies the currency unit (e.g., USD) in which the balance should be reported.

```json
{
"accountSetId": "e0cacaef-b692-48d6-81e2-238a5a614a04",
"journalId": "bcc24f47-990c-457d-88cb-76332450ac77",
"currency": "USD"
}
```
<GraphQLVariables variablesPath="/gql/variables/accountSetWithBalance.json" />

### GraphQL Request Body

```graphql
query accountSetWithBalance($accountSetId: UUID!, $currency: CurrencyCode!) {
accountSet(id: $accountSetId) {
name
journalId
balance(currency: $currency) {
settled {
normalBalance {
units
}
}
}
}
}
```
<GraphQLBody queryPath="/gql/account-set-with-balance.gql" />

### Response

```json
{
"data": {
"accountSet": {
"name": "Main Account Set",
"journalId": "bcc24f47-990c-457d-88cb-76332450ac77",
"balance": {
"settled": {
"normalBalance": {
"units": "9.53"
}
}
}
}
}
}
```
<GraphQLResponse responsePath="/gql/responses/accountSetWithBalanceResponse.json" />

## Significance

Expand Down
Loading

0 comments on commit e1b75d0

Please sign in to comment.