Last active
November 23, 2022 16:36
-
-
Save GarrickBee/182f060f126ffef510742c2ce3247de0 to your computer and use it in GitHub Desktop.
Example of Creating Transaction to WaveApp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { | |
| WAVE_BALANCE_TYPE, | |
| WAVE_ACCOUNT_ID, | |
| WAVE_TRANSACTION_DIRECTION, | |
| } from "@src/type/account.type"; | |
| import axios, { AxiosRequestConfig } from "axios"; | |
| import dayjs from "dayjs"; | |
| async function wavePost(data?: any, config?: AxiosRequestConfig) { | |
| return await axios.post("https://gql.waveapps.com/graphql/public", data, { | |
| headers: { | |
| Authorization: "Bearer " + process.env.WAVE_AUTH_TOKEN, | |
| "Content-Type": "application/json", | |
| }, | |
| ...config, | |
| }); | |
| } | |
| export async function createTransaction(param: { | |
| externalId: string; | |
| date: Date; | |
| description: string; | |
| anchor: { | |
| accountId: WAVE_ACCOUNT_ID; | |
| amount: number; | |
| direction: WAVE_TRANSACTION_DIRECTION; | |
| }; | |
| lineItems: { | |
| accountId: WAVE_ACCOUNT_ID; | |
| amount: number; | |
| balance: WAVE_BALANCE_TYPE; | |
| }[]; | |
| }) { | |
| const data = { | |
| query: `mutation ($input:MoneyTransactionCreateInput!){ | |
| moneyTransactionCreate(input:$input){ | |
| didSucceed | |
| inputErrors{ | |
| path | |
| message | |
| code | |
| } | |
| transaction{ | |
| id | |
| } | |
| } | |
| }`, | |
| variables: { | |
| input: { | |
| businessId: process.env.PERSONAL_BUSINESS_ID, | |
| externalId: param.externalId, | |
| date: dayjs(param.date).format("YYYY-MM-DD"), | |
| description: param.description || "No Description", | |
| anchor: param.anchor, | |
| lineItems: param.lineItems, | |
| }, | |
| }, | |
| }; | |
| return await wavePost(data).then( | |
| (res) => { | |
| const { data } = res.data; | |
| if (data.moneyTransactionCreate.didSucceed == false) { | |
| throw new Error( | |
| data?.moneyTransactionCreate?.inputErrors[0]?.message || | |
| "Error in inserting data to waveapps" | |
| ); | |
| } | |
| return res.data; | |
| } | |
| ); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment