@tomei/finance 0.1.0 → 0.1.2

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.
Files changed (126) hide show
  1. package/dist/account/account.d.ts +5 -5
  2. package/dist/account/account.js +27 -30
  3. package/dist/account/account.js.map +1 -1
  4. package/dist/account/account.repository.d.ts +1 -1
  5. package/dist/account/account.repository.js +2 -2
  6. package/dist/account/account.repository.js.map +1 -1
  7. package/dist/account/entities/account.entity.d.ts +0 -6
  8. package/dist/account/entities/account.entity.js +0 -26
  9. package/dist/account/entities/account.entity.js.map +1 -1
  10. package/dist/account/interfaces/account-attr.interface.d.ts +3 -3
  11. package/dist/account/interfaces/account.repository.interface.d.ts +1 -1
  12. package/dist/base/account-system.interface.d.ts +5 -5
  13. package/dist/base/base.address.d.ts +8 -0
  14. package/dist/base/base.address.js +15 -0
  15. package/dist/base/base.address.js.map +1 -0
  16. package/dist/base/base.object.d.ts +7 -0
  17. package/dist/base/base.object.js +14 -0
  18. package/dist/base/base.object.js.map +1 -0
  19. package/dist/base/base.owner.d.ts +23 -0
  20. package/dist/base/base.owner.js +34 -0
  21. package/dist/base/base.owner.js.map +1 -0
  22. package/dist/base/index.d.ts +4 -6
  23. package/dist/base/index.js +7 -5
  24. package/dist/base/index.js.map +1 -1
  25. package/dist/base/object/base.object.d.ts +7 -0
  26. package/dist/base/object/base.object.js +14 -0
  27. package/dist/base/object/base.object.js.map +1 -0
  28. package/dist/base/owner/base.owner.d.ts +22 -0
  29. package/dist/base/owner/base.owner.js +31 -0
  30. package/dist/base/owner/base.owner.js.map +1 -0
  31. package/dist/config.d.ts +33 -0
  32. package/dist/config.js +145 -0
  33. package/dist/config.js.map +1 -0
  34. package/dist/customer/customer.d.ts +27 -0
  35. package/dist/customer/customer.js +104 -0
  36. package/dist/customer/customer.js.map +1 -0
  37. package/dist/customer/customer.repository.d.ts +0 -0
  38. package/dist/customer/customer.repository.js +1 -0
  39. package/dist/customer/customer.repository.js.map +1 -0
  40. package/dist/customer/entities/customer.entity.d.ts +5 -0
  41. package/dist/customer/entities/customer.entity.js +41 -0
  42. package/dist/customer/entities/customer.entity.js.map +1 -0
  43. package/dist/customer/finance-customer.repository.d.ts +8 -0
  44. package/dist/customer/finance-customer.repository.js +31 -0
  45. package/dist/customer/finance-customer.repository.js.map +1 -0
  46. package/dist/customer/index.d.ts +7 -0
  47. package/dist/customer/index.js +10 -0
  48. package/dist/customer/index.js.map +1 -0
  49. package/dist/customer/interfaces/customer.repository.interface.d.ts +3 -0
  50. package/dist/customer/interfaces/customer.repository.interface.js +3 -0
  51. package/dist/customer/interfaces/customer.repository.interface.js.map +1 -0
  52. package/dist/customer/interfaces/finance-customer-attr.interface.d.ts +4 -0
  53. package/dist/customer/interfaces/finance-customer-attr.interface.js +3 -0
  54. package/dist/customer/interfaces/finance-customer-attr.interface.js.map +1 -0
  55. package/dist/customer/interfaces/finance-customer.repository.interface.d.ts +3 -0
  56. package/dist/customer/interfaces/finance-customer.repository.interface.js +3 -0
  57. package/dist/customer/interfaces/finance-customer.repository.interface.js.map +1 -0
  58. package/dist/index.d.ts +3 -0
  59. package/dist/index.js +5 -0
  60. package/dist/index.js.map +1 -1
  61. package/dist/interfaces/account-system.interface.d.ts +8 -0
  62. package/dist/interfaces/account-system.interface.js +3 -0
  63. package/dist/interfaces/account-system.interface.js.map +1 -0
  64. package/dist/interfaces/customer.repository.interface.d.ts +3 -0
  65. package/dist/interfaces/customer.repository.interface.js +3 -0
  66. package/dist/interfaces/customer.repository.interface.js.map +1 -0
  67. package/dist/interfaces/finance-customer.repository.interface.d.ts +3 -0
  68. package/dist/interfaces/finance-customer.repository.interface.js +3 -0
  69. package/dist/interfaces/finance-customer.repository.interface.js.map +1 -0
  70. package/dist/interfaces/index.d.ts +2 -0
  71. package/dist/interfaces/index.js +3 -0
  72. package/dist/interfaces/index.js.map +1 -0
  73. package/dist/interfaces/systemConfig.interface.d.ts +0 -0
  74. package/dist/interfaces/systemConfig.interface.js +1 -0
  75. package/dist/interfaces/systemConfig.interface.js.map +1 -0
  76. package/dist/quickbook-client/client.d.ts +13 -4
  77. package/dist/quickbook-client/client.js +62 -0
  78. package/dist/quickbook-client/client.js.map +1 -1
  79. package/dist/quickbook-client/index.d.ts +4 -4
  80. package/dist/quickbook-client/index.js +1 -5
  81. package/dist/quickbook-client/index.js.map +1 -1
  82. package/dist/quickbook-client/interfaces/quickbook-client-create-account-options.interface.d.ts +9 -0
  83. package/dist/quickbook-client/interfaces/quickbook-client-create-account-options.interface.js +3 -0
  84. package/dist/quickbook-client/interfaces/quickbook-client-create-account-options.interface.js.map +1 -0
  85. package/dist/quickbook-client/interfaces/quickbook-client-create-customer-options.interface.d.ts +7 -0
  86. package/dist/quickbook-client/interfaces/quickbook-client-create-customer-options.interface.js +3 -0
  87. package/dist/quickbook-client/interfaces/quickbook-client-create-customer-options.interface.js.map +1 -0
  88. package/dist/quickbook-client/interfaces/quickbook-client-options.interface.d.ts +1 -1
  89. package/dist/tsconfig.tsbuildinfo +1 -1
  90. package/migrations/finance-customer-migration.js +21 -0
  91. package/package.json +6 -3
  92. package/src/account/account.repository.ts +1 -1
  93. package/src/account/account.ts +40 -33
  94. package/src/account/entities/account.entity.ts +0 -32
  95. package/src/account/interfaces/account-attr.interface.ts +3 -3
  96. package/src/account/interfaces/account.repository.interface.ts +1 -1
  97. package/src/base/base.address.ts +21 -0
  98. package/src/base/base.object.ts +14 -0
  99. package/src/base/base.owner.ts +59 -0
  100. package/src/base/index.ts +4 -7
  101. package/src/config.ts +173 -0
  102. package/src/customer/customer.ts +146 -0
  103. package/src/customer/entities/customer.entity.ts +23 -0
  104. package/src/customer/finance-customer.repository.ts +20 -0
  105. package/src/customer/index.ts +15 -0
  106. package/src/customer/interfaces/customer.repository.interface.ts +3 -0
  107. package/src/customer/interfaces/finance-customer-attr.interface.ts +4 -0
  108. package/src/customer/interfaces/finance-customer.repository.interface.ts +3 -0
  109. package/src/index.ts +3 -0
  110. package/src/interfaces/account-system.interface.ts +8 -0
  111. package/src/interfaces/index.ts +3 -0
  112. package/src/quickbook-client/client.ts +71 -4
  113. package/src/quickbook-client/index.ts +6 -7
  114. package/src/quickbook-client/interfaces/quickbook-client-create-account-options.interface.ts +9 -0
  115. package/src/quickbook-client/interfaces/quickbook-client-create-customer-options.interface.ts +8 -0
  116. package/src/quickbook-client/interfaces/quickbook-client-options.interface.ts +1 -1
  117. package/src/base/account-system.interface.ts +0 -7
  118. package/src/base/base.repository.abstract.ts +0 -28
  119. package/src/base/base.repository.interface.ts +0 -9
  120. package/src/base/object/base.object.abstract.ts +0 -6
  121. package/src/base/object/object.interface.ts +0 -4
  122. package/src/base/owner/base.owner.abstract.ts +0 -21
  123. package/src/base/owner/owner.interface.ts +0 -20
  124. package/src/quickbook-client/constant.ts +0 -1
  125. package/src/quickbook-client/quickbook-client.module-definition.ts +0 -7
  126. package/src/quickbook-client/quickbook-client.module.ts +0 -24
@@ -0,0 +1,21 @@
1
+ 'use strict';
2
+
3
+ module.exports = {
4
+ async up(queryInterface, Sequelize) {
5
+ await queryInterface.createTable('finance_Customer', {
6
+ CustomerId: {
7
+ type: Sequelize.STRING(30),
8
+ primaryKey: true,
9
+ allowNull: false,
10
+ },
11
+ AccSystemId: {
12
+ type: Sequelize.STRING(30),
13
+ allowNull: false,
14
+ },
15
+ });
16
+ },
17
+
18
+ async down(queryInterface) {
19
+ await queryInterface.dropTable('finance_Customer');
20
+ },
21
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tomei/finance",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "description": "NestJS package for finance module",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -34,13 +34,13 @@
34
34
  "@nestjs/core": "^9.0.8",
35
35
  "@nestjs/platform-express": "^9.0.8",
36
36
  "@nestjs/sequelize": "^9.0.0",
37
+ "axios": "^1.1.3",
37
38
  "class-transformer": "^0.5.1",
38
39
  "class-validator": "^0.13.2",
39
40
  "dotenv": "^16.0.3",
40
41
  "intuit-oauth": "^4.0.0",
41
42
  "sequelize-typescript": "^2.1.3",
42
- "uniqid": "^5.4.0",
43
- "axios": "^1.1.3"
43
+ "uniqid": "^5.4.0"
44
44
  },
45
45
  "devDependencies": {
46
46
  "@nestjs/swagger": "^6.0.4",
@@ -68,5 +68,8 @@
68
68
  "npm run build",
69
69
  "git add ."
70
70
  ]
71
+ },
72
+ "dependencies": {
73
+ "@tomei/general": "^0.0.7"
71
74
  }
72
75
  }
@@ -1,7 +1,7 @@
1
1
  import { Injectable } from '@nestjs/common';
2
2
  import { InjectModel } from '@nestjs/sequelize';
3
- import { BaseRepository } from '../base/base.repository.abstract';
4
3
  import { AccountModel } from './entities/account.entity';
4
+ import { BaseRepository } from '@tomei/general';
5
5
  import {
6
6
  IAccountAttr,
7
7
  ICreateAccountAttr,
@@ -1,8 +1,11 @@
1
- import { ObjectBase, OwnerBase } from 'src/base';
1
+ import { RelatedObject, Owner } from '../base';
2
2
  import { ICreateAccountAttr } from './interfaces/account-attr.interface';
3
3
  import { IAccountRepository } from './interfaces/account.repository.interface';
4
- import { QuickBookClient } from '../quickbook-client';
5
4
  import * as uniqid from 'uniqid';
5
+ import { AccountModel } from './entities/account.entity';
6
+ import axios from 'axios';
7
+ import { getConfig } from '../config';
8
+ import { IAccountSystem } from 'src/interfaces';
6
9
  export class Account {
7
10
  accountRepository: IAccountRepository;
8
11
 
@@ -13,8 +16,8 @@ export class Account {
13
16
  private AccSystemId: string;
14
17
 
15
18
  ParentAccountNo: string;
16
- Owner: OwnerBase;
17
- RelatedObject: ObjectBase;
19
+ Owner: Owner;
20
+ RelatedObject: RelatedObject;
18
21
  AccountType: string;
19
22
  AccountSubType: string;
20
23
 
@@ -28,7 +31,7 @@ export class Account {
28
31
  params?: ICreateAccountAttr,
29
32
  ) {
30
33
  this.accountRepository = accountRepository;
31
- this.AccSystemId = SysCode;
34
+ this.AccSystemId = getConfig().systemConfig[SysCode].accountingSystem;
32
35
  if (params) {
33
36
  this.init(params);
34
37
  }
@@ -43,10 +46,15 @@ export class Account {
43
46
  if (params.ParentAccountNo) {
44
47
  this.ParentAccountNo = params.ParentAccountNo;
45
48
  }
49
+
50
+ this.OwnerId = params.Owner.Id;
51
+ this.OwnerType = typeof params.Owner;
52
+ this.RelatedObjectId = params.RelatedObject.Id;
53
+ this.RelatedObjectType = typeof params.RelatedObject;
46
54
  this.isParamsInitialized = true;
47
55
  }
48
56
 
49
- async create(userId: string, client: QuickBookClient, dbOptions: any) {
57
+ async create(userId: string, client: IAccountSystem, dbOptions: any) {
50
58
  try {
51
59
  if (!this.Owner || !this.RelatedObject) {
52
60
  throw new Error(
@@ -83,51 +91,33 @@ export class Account {
83
91
  );
84
92
  }
85
93
 
86
- const companyId = client.getCompanyId();
87
- const createAccountPayload = {
94
+ let createAccountPayload: any = {
88
95
  Name: this.Owner.FullName,
89
96
  AcctNum: uniqid(),
90
97
  AccountType: this.AccountType,
91
98
  AccountSubType: this.AccountSubType,
92
99
  };
93
- const response = await client.callApi({
94
- endpoint: `/v3/company/${companyId}/account`,
95
- method: 'POST',
96
- headers: {
97
- 'Content-Type': 'application/json',
98
- },
99
- body: JSON.stringify(createAccountPayload),
100
- });
101
-
102
- const data = response.json.Account;
103
100
 
104
101
  if (this.ParentAccountNo) {
105
- const createChildAccountPayload = {
106
- Id: data.Id,
102
+ createAccountPayload = {
103
+ ...createAccountPayload,
107
104
  CurrencyRef: 'MYR',
108
105
  ParentRef: this.ParentAccountNo,
109
106
  SubAccount: true,
110
107
  };
111
-
112
- await client.callApi({
113
- endpoint: `/v3/company/${companyId}/account`,
114
- method: 'PUT',
115
- headers: {
116
- 'Content-Type': 'application/json',
117
- },
118
- body: JSON.stringify(createChildAccountPayload),
119
- });
120
108
  }
121
109
 
110
+ const data = await client.createAccount(createAccountPayload);
111
+
122
112
  const account = await this.accountRepository.create(
123
113
  {
124
114
  AccountNo: data.Id,
125
115
  SystemCode: this.AccSystemId,
126
116
  Name: data.AcctNum,
127
- OwnerId: this.Owner.Id,
128
- OwnerType: this.Owner.Type,
129
- RelatedObjectId: this.RelatedObject.Id,
130
- RelatedObjectType: this.RelatedObject.Type,
117
+ OwnerId: this.OwnerId,
118
+ OwnerType: this.OwnerType,
119
+ RelatedObjectId: this.RelatedObjectId,
120
+ RelatedObjectType: this.RelatedObjectType,
131
121
  CreatedAt: new Date(),
132
122
  CreatedById: userId,
133
123
  UpdatedAt: new Date(),
@@ -136,6 +126,23 @@ export class Account {
136
126
  dbOptions,
137
127
  );
138
128
 
129
+ const payload = {
130
+ Action: 'Create',
131
+ Activity: 'Account Created',
132
+ Description: `Account (ID: ${account.AccountNo}) has been created`,
133
+ EntityType: typeof AccountModel,
134
+ EntityValueBefore: JSON.stringify({}),
135
+ EntityValueAfter: JSON.stringify(account),
136
+ PerformedById: account.CreatedById,
137
+ PerformedAt: new Date(),
138
+ EntityId: account.AccountNo,
139
+ };
140
+
141
+ await axios.post(
142
+ `${process.env.COMMON_API_URL}/activity-histories`,
143
+ payload,
144
+ );
145
+
139
146
  return account;
140
147
  } catch (error) {
141
148
  throw error;
@@ -6,11 +6,7 @@ import {
6
6
  Table,
7
7
  DataType,
8
8
  UpdatedAt,
9
- AfterCreate,
10
9
  } from 'sequelize-typescript';
11
- import { ObjectBase, OwnerBase } from '../../base';
12
- import { QuickBookClient } from '../../quickbook-client';
13
- import axios from 'axios';
14
10
 
15
11
  @Table({ tableName: 'finance_Account', createdAt: false, updatedAt: false })
16
12
  export class AccountModel extends Model {
@@ -103,32 +99,4 @@ export class AccountModel extends Model {
103
99
  })
104
100
  @Column({ allowNull: false, type: DataType.STRING(30) })
105
101
  UpdatedById: string;
106
-
107
- Owner: OwnerBase;
108
- RelatedObject: ObjectBase;
109
- QuickbookClient: QuickBookClient;
110
-
111
- @AfterCreate
112
- static async createActivityHistory(instance) {
113
- const payload = {
114
- Action: 'Create',
115
- Activity: 'Account Created',
116
- Description: `Account (ID: ${instance.AccountNo}) has been created`,
117
- EntityType: typeof instance,
118
- EntityValueBefore: JSON.stringify({}),
119
- EntityValueAfter: JSON.stringify(instance),
120
- PerformedById: instance.CreatedById,
121
- PerformedAt: new Date(),
122
- EntityId: instance.AccountNo,
123
- };
124
-
125
- try {
126
- await axios.post(
127
- `${process.env.COMMON_API_URL}/activity-histories`,
128
- payload,
129
- );
130
- } catch (error) {
131
- throw error;
132
- }
133
- }
134
102
  }
@@ -1,4 +1,4 @@
1
- import { ObjectBase, OwnerBase } from '../../base';
1
+ import { Owner, RelatedObject } from 'src/base';
2
2
 
3
3
  export interface IAccountAttr {
4
4
  AccountNo: string;
@@ -15,8 +15,8 @@ export interface IAccountAttr {
15
15
  }
16
16
 
17
17
  export interface ICreateAccountAttr {
18
- Owner: OwnerBase;
19
- RelatedObject: ObjectBase;
18
+ Owner: Owner;
19
+ RelatedObject: RelatedObject;
20
20
  AccountType: string;
21
21
  AccountSubType: string;
22
22
  ParentAccountNo?: string;
@@ -1,4 +1,4 @@
1
- import { IBaseRepository } from '../../base/base.repository.interface';
1
+ import { IBaseRepository } from '@tomei/general';
2
2
  import { AccountModel } from '../entities/account.entity';
3
3
 
4
4
  export type IAccountRepository = IBaseRepository<AccountModel>;
@@ -0,0 +1,21 @@
1
+ import { AddressBase } from '@tomei/general';
2
+
3
+ export class Address extends AddressBase {
4
+ Line1: string;
5
+ City: string;
6
+ PostalCode: string;
7
+ Country: string;
8
+
9
+ constructor(
10
+ line1: string,
11
+ city: string,
12
+ postalCode: string,
13
+ country: string,
14
+ ) {
15
+ super();
16
+ this.Line1 = line1;
17
+ this.City = city;
18
+ this.PostalCode = postalCode;
19
+ this.Country = country;
20
+ }
21
+ }
@@ -0,0 +1,14 @@
1
+ import { ObjectBase } from '@tomei/general';
2
+
3
+ export class RelatedObject extends ObjectBase {
4
+ Id: string;
5
+ Name: string;
6
+ Type: string;
7
+
8
+ constructor(id: string, name: string) {
9
+ super();
10
+ this.Id = id;
11
+ this.Name = name;
12
+ this.Type = 'RelatedObject';
13
+ }
14
+ }
@@ -0,0 +1,59 @@
1
+ import { PersonBase, AddressBase } from '@tomei/general';
2
+
3
+ export class Owner extends PersonBase {
4
+ FullName: string;
5
+ IDNo: string;
6
+ IDType: string;
7
+ Email: string;
8
+ ContactNo: string;
9
+ Address: AddressBase;
10
+ Id: string;
11
+ Name: string;
12
+
13
+ constructor(
14
+ fullName: string,
15
+ idNo: string,
16
+ idType: string,
17
+ email: string,
18
+ contactNo: string,
19
+ address: AddressBase,
20
+ id: string,
21
+ name: string,
22
+ ) {
23
+ super();
24
+ this.FullName = fullName;
25
+ this.IDNo = idNo;
26
+ this.IDType = idType;
27
+ this.Email = email;
28
+ this.ContactNo = contactNo;
29
+ this.Address = address;
30
+ this.Id = id;
31
+ this.Name = name;
32
+ }
33
+
34
+ getDetails(): {
35
+ FullName: string;
36
+ IDNo: string;
37
+ IDType: string;
38
+ Email: string;
39
+ ContactNo: string;
40
+ Address: AddressBase;
41
+ Id: string;
42
+ Name: string;
43
+ } {
44
+ return {
45
+ FullName: this.FullName,
46
+ IDNo: this.IDNo,
47
+ IDType: this.IDType,
48
+ Email: this.Email,
49
+ ContactNo: this.ContactNo,
50
+ Address: this.Address,
51
+ Id: this.Id,
52
+ Name: this.Name,
53
+ };
54
+ }
55
+
56
+ getDefaultAddress() {
57
+ return this.Address;
58
+ }
59
+ }
package/src/base/index.ts CHANGED
@@ -1,7 +1,4 @@
1
- import { IAccountSystem } from './account-system.interface';
2
- import { IOwner } from './owner/owner.interface';
3
- import { OwnerBase } from './owner/base.owner.abstract';
4
- import { IObject } from './object/object.interface';
5
- import { ObjectBase } from './object/base.object.abstract';
6
-
7
- export { IAccountSystem, ObjectBase, OwnerBase, IObject, IOwner };
1
+ import { RelatedObject } from './base.object';
2
+ import { Owner } from './base.owner';
3
+ import { Address } from './base.address';
4
+ export { RelatedObject, Owner, Address };
package/src/config.ts ADDED
@@ -0,0 +1,173 @@
1
+ const config = {
2
+ development: {
3
+ commonApiUrl: process.env.COMMON_API_URL,
4
+ accountingSystem: {
5
+ quickbooks: {
6
+ packageName: process.env.PACKAGE_NAME,
7
+ accountSystemClass: process.env.ACCOUNT_SYSTEM_CLASS,
8
+ API_Url: process.env.API_URL,
9
+ },
10
+ xero: {
11
+ packageName: process.env.PACKAGE_NAME,
12
+ accountSystemClass: process.env.ACCOUNT_SYSTEM_CLASS,
13
+ API_Url: process.env.API_URL,
14
+ },
15
+ },
16
+ systemConfig: {
17
+ EZC: {
18
+ accountingSystem: 'quickbooks',
19
+ companyName: process.env.COMPANY_NAME,
20
+ API_Key: process.env.QUICKBOOK_API_KEY,
21
+ API_Secret: process.env.QUICKBOOK_API_SECRET,
22
+ redirectUrl: process.env.EZCASH_REDIRECT_URL,
23
+ },
24
+ EZG: {
25
+ accountingSystem: 'xero',
26
+ companyName: process.env.COMPANY_NAME,
27
+ API_Key: process.env.QUICKBOOK_API_KEY,
28
+ API_Secret: process.env.QUICKBOOK_API_SECRET,
29
+ redirectUrl: process.env.EZGOLD_REDIRECT_URL,
30
+ },
31
+ },
32
+ },
33
+ test: {
34
+ commonApiUrl: process.env.COMMON_API_URL,
35
+ accountingSystem: {
36
+ quickbooks: {
37
+ packageName: process.env.PACKAGE_NAME,
38
+ accountSystemClass: process.env.ACCOUNT_SYSTEM_CLASS,
39
+ API_Url: process.env.API_URL,
40
+ },
41
+ xero: {
42
+ packageName: process.env.PACKAGE_NAME,
43
+ accountSystemClass: process.env.ACCOUNT_SYSTEM_CLASS,
44
+ API_Url: process.env.API_URL,
45
+ },
46
+ },
47
+ systemConfig: {
48
+ EZC: {
49
+ accountingSystem: 'quickbooks',
50
+ companyName: process.env.COMPANY_NAME,
51
+ API_Key: process.env.QUICKBOOK_API_KEY,
52
+ API_Secret: process.env.QUICKBOOK_API_SECRET,
53
+ redirectUrl: process.env.EZCASH_REDIRECT_URL,
54
+ },
55
+ EZG: {
56
+ accountingSystem: 'xero',
57
+ companyName: process.env.COMPANY_NAME,
58
+ API_Key: process.env.QUICKBOOK_API_KEY,
59
+ API_Secret: process.env.QUICKBOOK_API_SECRET,
60
+ redirectUrl: process.env.EZGOLD_REDIRECT_URL,
61
+ },
62
+ },
63
+ },
64
+ staging: {
65
+ commonApiUrl: process.env.COMMON_API_URL,
66
+ accountingSystem: {
67
+ quickbooks: {
68
+ packageName: process.env.PACKAGE_NAME,
69
+ accountSystemClass: process.env.ACCOUNT_SYSTEM_CLASS,
70
+ API_Url: process.env.API_URL,
71
+ },
72
+ xero: {
73
+ packageName: process.env.PACKAGE_NAME,
74
+ accountSystemClass: process.env.ACCOUNT_SYSTEM_CLASS,
75
+ API_Url: process.env.API_URL,
76
+ },
77
+ },
78
+ systemConfig: {
79
+ EZC: {
80
+ accountingSystem: 'quickbooks',
81
+ companyName: process.env.COMPANY_NAME,
82
+ API_Key: process.env.QUICKBOOK_API_KEY,
83
+ API_Secret: process.env.QUICKBOOK_API_SECRET,
84
+ redirectUrl: process.env.EZCASH_REDIRECT_URL,
85
+ },
86
+ EZG: {
87
+ accountingSystem: 'xero',
88
+ companyName: process.env.COMPANY_NAME,
89
+ API_Key: process.env.QUICKBOOK_API_KEY,
90
+ API_Secret: process.env.QUICKBOOK_API_SECRET,
91
+ redirectUrl: process.env.EZGOLD_REDIRECT_URL,
92
+ },
93
+ },
94
+ },
95
+ production: {
96
+ commonApiUrl: process.env.COMMON_API_URL,
97
+ accountingSystem: {
98
+ quickbooks: {
99
+ packageName: process.env.PACKAGE_NAME,
100
+ accountSystemClass: process.env.ACCOUNT_SYSTEM_CLASS,
101
+ API_Url: process.env.API_URL,
102
+ },
103
+ xero: {
104
+ packageName: process.env.PACKAGE_NAME,
105
+ accountSystemClass: process.env.ACCOUNT_SYSTEM_CLASS,
106
+ API_Url: process.env.API_URL,
107
+ },
108
+ },
109
+ systemConfig: {
110
+ EZC: {
111
+ accountingSystem: 'quickbooks',
112
+ companyName: process.env.COMPANY_NAME,
113
+ API_Key: process.env.QUICKBOOK_API_KEY,
114
+ API_Secret: process.env.QUICKBOOK_API_SECRET,
115
+ redirectUrl: process.env.EZCASH_REDIRECT_URL,
116
+ },
117
+ EZG: {
118
+ accountingSystem: 'xero',
119
+ companyName: process.env.COMPANY_NAME,
120
+ API_Key: process.env.QUICKBOOK_API_KEY,
121
+ API_Secret: process.env.QUICKBOOK_API_SECRET,
122
+ redirectUrl: process.env.EZGOLD_REDIRECT_URL,
123
+ },
124
+ },
125
+ },
126
+ };
127
+
128
+ type Config = {
129
+ commonApiUrl: string;
130
+ accountingSystem: {
131
+ quickbooks: {
132
+ packageName: string;
133
+ accountSystemClass: string;
134
+ API_Url: string;
135
+ };
136
+ xero: {
137
+ packageName: string;
138
+ accountSystemClass: string;
139
+ API_Url: string;
140
+ };
141
+ };
142
+ systemConfig: {
143
+ EZC: {
144
+ accountingSystem: string;
145
+ companyName: string;
146
+ API_Key: string;
147
+ API_Secret: string;
148
+ redirectUrl: string;
149
+ };
150
+ EZG: {
151
+ accountingSystem: string;
152
+ companyName: string;
153
+ API_Key: string;
154
+ API_Secret: string;
155
+ redirectUrl: string;
156
+ };
157
+ };
158
+ };
159
+
160
+ export const getConfig = (): Config => {
161
+ switch (process.env.NODE_ENV) {
162
+ case 'development':
163
+ return config['development'];
164
+ case 'test':
165
+ return config['test'];
166
+ case 'staging':
167
+ return config['staging'];
168
+ case 'production':
169
+ return config['production'];
170
+ default:
171
+ break;
172
+ }
173
+ };
@@ -0,0 +1,146 @@
1
+ import { AddressBase, PersonBase } from '@tomei/general';
2
+ import { IAccountSystem } from '../interfaces';
3
+ import { Address } from '../base/base.address';
4
+ import { FinanceCustomerRepository } from './finance-customer.repository';
5
+ import { ICustomerRepository } from './interfaces/customer.repository.interface';
6
+ import { getConfig } from '../config';
7
+
8
+ export class Customer extends PersonBase {
9
+ private CustomerId: string;
10
+ private AccSystemId: string;
11
+
12
+ FullName: string;
13
+ IDNo: string;
14
+ IDType: string;
15
+ Email: string;
16
+ ContactNo: string;
17
+ Address: AddressBase;
18
+ Id: string;
19
+ Name: string;
20
+
21
+ financeCustomerRepository: FinanceCustomerRepository;
22
+ customerRepository: ICustomerRepository<any>;
23
+ customerAddressRepository: any;
24
+ client: IAccountSystem;
25
+ person: PersonBase;
26
+ systemCode: string;
27
+ constructor(
28
+ financeCustomerRepository: FinanceCustomerRepository,
29
+ customerRepository: ICustomerRepository<any>,
30
+ customerAddressRepository: any,
31
+ client: IAccountSystem,
32
+ systemCode: string,
33
+ person?: PersonBase,
34
+ ) {
35
+ super();
36
+ this.financeCustomerRepository = financeCustomerRepository;
37
+ this.customerRepository = customerRepository;
38
+ this.customerAddressRepository = customerAddressRepository;
39
+ this.client = client;
40
+ this.systemCode = systemCode;
41
+ if (person) {
42
+ this.person = person;
43
+ this.init(person);
44
+ }
45
+ }
46
+
47
+ init(person: PersonBase) {
48
+ this.FullName = person.FullName;
49
+ this.IDNo = person.IDNo;
50
+ this.IDType = person.IDType;
51
+ this.Email = person.Email;
52
+ this.ContactNo = person.ContactNo;
53
+ this.Address = person.Address;
54
+ }
55
+
56
+ async getDetails(): Promise<any> {
57
+ if (
58
+ !this.FullName ||
59
+ !this.IDNo ||
60
+ !this.IDType ||
61
+ !this.Email ||
62
+ !this.ContactNo ||
63
+ !this.Address
64
+ ) {
65
+ const customerData = await this.customerRepository.findOne({
66
+ where: { IdNo: this.IDNo },
67
+ });
68
+ if (!customerData) {
69
+ throw new Error('Customer must be created first.');
70
+ }
71
+
72
+ this.FullName = customerData.FullName;
73
+ this.IDNo = customerData.IDNo;
74
+ this.IDType = customerData.IDType;
75
+ this.Email = customerData.Email;
76
+ this.ContactNo = customerData.ContactNo;
77
+ this.Id = customerData.CustomerId;
78
+ this.Name = customerData.PreferredName;
79
+ }
80
+
81
+ return {
82
+ FullName: this.FullName,
83
+ IDNo: this.IDNo,
84
+ IDType: this.IDType,
85
+ Email: this.Email,
86
+ ContactNo: this.ContactNo,
87
+ Address: this.Address,
88
+ Id: this.Id,
89
+ Name: this.Name,
90
+ };
91
+ }
92
+
93
+ async getDefaultAddress(): Promise<any> {
94
+ if (
95
+ !this.Address.Line1 ||
96
+ !this.Address.City ||
97
+ !this.Address.PostalCode ||
98
+ !this.Address.Country
99
+ ) {
100
+ const addressData = await this.customerAddressRepository.findOne({
101
+ where: { CustomerId: this.Id, isDefaultYN: 'Y' },
102
+ });
103
+
104
+ if (!addressData) {
105
+ throw new Error('Address must be created first.');
106
+ }
107
+
108
+ this.Address = new Address(
109
+ addressData.Address,
110
+ addressData.City,
111
+ addressData.PostCode,
112
+ addressData.Country,
113
+ );
114
+ }
115
+ return {
116
+ Line1: this.Address.Line1,
117
+ City: this.Address.City,
118
+ PostalCode: this.Address.PostalCode,
119
+ Country: this.Address.Country,
120
+ };
121
+ }
122
+
123
+ async create() {
124
+ try {
125
+ await this.getDetails();
126
+ await this.getDefaultAddress();
127
+
128
+ const customerData = await this.client.createCustomer({
129
+ FullName: this.FullName,
130
+ Email: this.Email,
131
+ ContractNo: this.ContactNo,
132
+ Address: this.Address,
133
+ });
134
+
135
+ this.AccSystemId =
136
+ getConfig().systemConfig[this.systemCode].accountingSystem;
137
+ this.CustomerId = customerData.Id;
138
+ return await this.financeCustomerRepository.create({
139
+ CustomerId: customerData.Id,
140
+ AccSystemId: this.AccSystemId,
141
+ });
142
+ } catch (error) {
143
+ throw error;
144
+ }
145
+ }
146
+ }