@tomei/finance 0.0.19 → 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/dist/account/account.d.ts +4 -2
- package/dist/account/account.js +73 -33
- package/dist/account/account.js.map +1 -1
- package/dist/account/entities/account.entity.d.ts +6 -1
- package/dist/account/entities/account.entity.js +1 -1
- package/dist/account/entities/account.entity.js.map +1 -1
- package/dist/account/interfaces/account-attr.interface.d.ts +2 -1
- package/dist/quickbook-client/client.js +6 -2
- package/dist/quickbook-client/client.js.map +1 -1
- package/dist/quickbook-client/interfaces/quickbook-client-call-options.interface.d.ts +2 -2
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/src/account/account.ts +91 -48
- package/src/account/entities/account.entity.ts +7 -1
- package/src/account/interfaces/account-attr.interface.ts +2 -1
- package/src/quickbook-client/client.ts +11 -2
- package/src/quickbook-client/interfaces/quickbook-client-call-options.interface.ts +2 -2
package/package.json
CHANGED
package/src/account/account.ts
CHANGED
|
@@ -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
|
-
|
|
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
|
-
|
|
40
|
-
|
|
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
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
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
|
-
|
|
57
|
-
|
|
58
|
-
|
|
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
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
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
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
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
|
-
|
|
80
|
-
|
|
81
|
-
|
|
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
|
-
|
|
135
|
+
return account;
|
|
136
|
+
} catch (error) {
|
|
137
|
+
throw error;
|
|
138
|
+
}
|
|
96
139
|
}
|
|
97
140
|
}
|
|
@@ -8,6 +8,8 @@ import {
|
|
|
8
8
|
UpdatedAt,
|
|
9
9
|
BeforeValidate,
|
|
10
10
|
} from 'sequelize-typescript';
|
|
11
|
+
import { ObjectBase, OwnerBase } from '../../base';
|
|
12
|
+
import { QuickBookClient } from '../../quickbook-client';
|
|
11
13
|
|
|
12
14
|
@Table({ tableName: 'finance_Account', createdAt: false, updatedAt: false })
|
|
13
15
|
export class AccountModel extends Model {
|
|
@@ -101,8 +103,12 @@ export class AccountModel extends Model {
|
|
|
101
103
|
@Column({ allowNull: false, type: DataType.STRING(30) })
|
|
102
104
|
UpdatedById: string;
|
|
103
105
|
|
|
106
|
+
Owner: OwnerBase;
|
|
107
|
+
RelatedObject: ObjectBase;
|
|
108
|
+
QuickbookClient: QuickBookClient;
|
|
109
|
+
|
|
104
110
|
@BeforeValidate
|
|
105
|
-
static beforeValidateProcess(instance:
|
|
111
|
+
static beforeValidateProcess(instance: AccountModel) {
|
|
106
112
|
console.log(instance, '<<<<<<<');
|
|
107
113
|
}
|
|
108
114
|
}
|
|
@@ -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
|
-
|
|
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) {
|