@tomei/finance 0.0.20 → 0.0.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tomei/finance",
3
- "version": "0.0.20",
3
+ "version": "0.0.21",
4
4
  "description": "NestJS package for finance module",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -2,6 +2,7 @@ import { ObjectBase, OwnerBase } from 'src/base';
2
2
  import { ICreateAccountAttr } from './interfaces/account-attr.interface';
3
3
  import { IAccountRepository } from './interfaces/account.repository.interface';
4
4
  import { QuickBookClient } from '../quickbook-client';
5
+ import * as cuid from 'cuid';
5
6
  export class Account {
6
7
  accountRepository: IAccountRepository;
7
8
  intuitClient: any;
@@ -12,9 +13,11 @@ export class Account {
12
13
  private RelatedObjectType: string;
13
14
  private AccSystemId: string;
14
15
 
15
- parentAccountNo: string;
16
+ ParentAccountNo: string;
16
17
  Owner: OwnerBase;
17
18
  RelatedObject: ObjectBase;
19
+ AccountType: string;
20
+ AccountSubType: string;
18
21
 
19
22
  isParamsInitialized: boolean;
20
23
  accessToken: string;
@@ -35,63 +38,103 @@ export class Account {
35
38
  init(params: ICreateAccountAttr) {
36
39
  this.Owner = params.Owner;
37
40
  this.RelatedObject = params.RelatedObject;
41
+ this.AccountType = params.AccountType;
42
+ this.AccountSubType = params.AccountSubType;
38
43
 
39
- const ownerData = this.Owner.getDetails();
40
- if (!ownerData) {
41
- throw new Error(
42
- 'Please save owner information in the database before creating an account',
43
- );
44
+ if (params.ParentAccountNo) {
45
+ this.ParentAccountNo = params.ParentAccountNo;
44
46
  }
47
+ this.isParamsInitialized = true;
48
+ }
49
+
50
+ async create(userId: string, client: QuickBookClient, dbOptions: any) {
51
+ try {
52
+ if (!this.Owner || !this.RelatedObject) {
53
+ throw new Error(
54
+ 'Owner must be set before creating an account." or "RelatedObject must be set before creating an account.',
55
+ );
56
+ }
45
57
 
46
- let isFull = true;
47
- for (const key in ownerData) {
48
- if (ownerData.hasOwnProperty(key)) {
49
- if (!ownerData[key]) {
50
- isFull = false;
51
- break;
58
+ if (!this.isParamsInitialized) {
59
+ throw new Error(
60
+ 'Account parameters must be initialized before creating an account.',
61
+ );
62
+ }
63
+
64
+ const ownerData = this.Owner.getDetails();
65
+ if (!ownerData) {
66
+ throw new Error(
67
+ 'Please save owner information in the database before creating an account',
68
+ );
69
+ }
70
+
71
+ let isFull = true;
72
+ for (const key in ownerData) {
73
+ if (ownerData.hasOwnProperty(key)) {
74
+ if (!ownerData[key]) {
75
+ isFull = false;
76
+ break;
77
+ }
52
78
  }
53
79
  }
54
- }
55
80
 
56
- if (!isFull) {
57
- throw new Error(
58
- 'Please save owner information in the database before creating an account',
59
- );
60
- }
81
+ if (!isFull) {
82
+ throw new Error(
83
+ 'Please save owner information in the database before creating an account',
84
+ );
85
+ }
61
86
 
62
- this.OwnerId = params.Owner.Id;
63
- this.OwnerType = typeof this.Owner;
64
- this.RelatedObjectId = params.RelatedObject.Id;
65
- this.RelatedObjectType = typeof this.RelatedObject;
66
- if (params.ParentAccountNo) {
67
- this.parentAccountNo = params.ParentAccountNo;
68
- }
69
- this.isParamsInitialized = true;
70
- }
87
+ await client.validateAndRefreshToken();
88
+ const createAccountPayload = {
89
+ Name: this.Owner.FullName,
90
+ AcctNum: cuid(),
91
+ AccountType: this.AccountType,
92
+ AccountSubType: this.AccountSubType,
93
+ };
94
+ const response = await client.callApi({
95
+ endpoint: `v3/company/company/${client.getCompanyId}/account`,
96
+ method: 'POST',
97
+ headers: {
98
+ 'Content-Type': 'application/json',
99
+ },
100
+ body: JSON.stringify(createAccountPayload),
101
+ });
71
102
 
72
- async create(client: QuickBookClient, dbOptions: any) {
73
- if (!this.Owner || !this.RelatedObject) {
74
- throw new Error(
75
- 'Owner must be set before creating an account." or "RelatedObject must be set before creating an account.',
76
- );
77
- }
103
+ if (this.ParentAccountNo) {
104
+ const createChildAccountPayload = {
105
+ Id: response.Account.Id,
106
+ CurrencyRef: 'MYR',
107
+ ParentRef: this.ParentAccountNo,
108
+ SubAccount: true,
109
+ };
78
110
 
79
- if (!this.isParamsInitialized) {
80
- throw new Error(
81
- 'Account parameters must be initialized before creating an account.',
111
+ await client.callApi({
112
+ endpoint: `v3/company/company/${client.getCompanyId}/account`,
113
+ method: 'PUT',
114
+ headers: {
115
+ 'Content-Type': 'application/json',
116
+ },
117
+ body: JSON.stringify(createChildAccountPayload),
118
+ });
119
+ }
120
+ const account = await this.accountRepository.create(
121
+ {
122
+ AccountNo: response.Account.Id,
123
+ SystemCode: this.AccSystemId,
124
+ Name: response.Account.AcctNum,
125
+ OwnerId: this.Owner.Id,
126
+ OwnerType: this.Owner.Type,
127
+ RelatedObjectId: this.RelatedObject.Id,
128
+ RelatedObjectType: this.RelatedObject.Type,
129
+ CreatedAt: new Date(),
130
+ CreatedById: userId,
131
+ },
132
+ dbOptions,
82
133
  );
83
- }
84
-
85
- const account = await this.accountRepository.create(
86
- {
87
- Owner: this.Owner,
88
- RelatedObject: this.RelatedObject,
89
- Client: client,
90
- ParentAccountNo: this.parentAccountNo,
91
- },
92
- dbOptions,
93
- );
94
134
 
95
- return account;
135
+ return account;
136
+ } catch (error) {
137
+ throw error;
138
+ }
96
139
  }
97
140
  }
@@ -17,6 +17,7 @@ export interface IAccountAttr {
17
17
  export interface ICreateAccountAttr {
18
18
  Owner: OwnerBase;
19
19
  RelatedObject: ObjectBase;
20
- Client?: any;
20
+ AccountType: string;
21
+ AccountSubType: string;
21
22
  ParentAccountNo?: string;
22
23
  }
@@ -91,11 +91,20 @@ export class QuickBookClient {
91
91
 
92
92
  async callApi(options: IQuickBookAPICallOptions): Promise<any> {
93
93
  try {
94
+ const headers = {
95
+ 'Content-Type': 'application/json',
96
+ };
97
+
94
98
  const response = this.quickBookClient.makeApiCall({
95
99
  url: this.apiUrl + options.endpoint,
96
100
  method: options.method,
97
- headers: options.headers,
98
- body: options.body,
101
+ headers: options.headers
102
+ ? {
103
+ ...headers,
104
+ ...options.headers,
105
+ }
106
+ : headers,
107
+ body: options.body ? options.body : '',
99
108
  });
100
109
  return response;
101
110
  } catch (error) {
@@ -1,6 +1,6 @@
1
1
  export interface IQuickBookAPICallOptions {
2
2
  endpoint: string;
3
3
  method: 'GET' | 'POST' | 'PUT' | 'DELETE';
4
- headers: object;
5
- body: object;
4
+ headers?: object;
5
+ body?: string;
6
6
  }