linker-api 1.0.0

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 (47) hide show
  1. package/README.md +1 -0
  2. package/dist/lib/config/link-config.js +30 -0
  3. package/dist/lib/config/link-config.js.map +1 -0
  4. package/dist/lib/database/database.js +61 -0
  5. package/dist/lib/database/database.js.map +1 -0
  6. package/dist/lib/database/mssql.js +72 -0
  7. package/dist/lib/database/mssql.js.map +1 -0
  8. package/dist/lib/database/mssql2.js +83 -0
  9. package/dist/lib/database/mssql2.js.map +1 -0
  10. package/dist/lib/database/prepare-call.js +45 -0
  11. package/dist/lib/database/prepare-call.js.map +1 -0
  12. package/dist/lib/database/prepared-stmt.js +92 -0
  13. package/dist/lib/database/prepared-stmt.js.map +1 -0
  14. package/dist/lib/database/schema/common-schema.js +81 -0
  15. package/dist/lib/database/schema/common-schema.js.map +1 -0
  16. package/dist/lib/database/schema/common-service.js +258 -0
  17. package/dist/lib/database/schema/common-service.js.map +1 -0
  18. package/dist/lib/database/schema/table-catelog.js +74 -0
  19. package/dist/lib/database/schema/table-catelog.js.map +1 -0
  20. package/dist/lib/database/transaction.js +18 -0
  21. package/dist/lib/database/transaction.js.map +1 -0
  22. package/dist/lib/dispatcher/health-service.js +135 -0
  23. package/dist/lib/dispatcher/health-service.js.map +1 -0
  24. package/dist/lib/dispatcher/i-dispatcher.js +191 -0
  25. package/dist/lib/dispatcher/i-dispatcher.js.map +1 -0
  26. package/dist/lib/dispatcher/table-catelog.js +74 -0
  27. package/dist/lib/dispatcher/table-catelog.js.map +1 -0
  28. package/dist/lib/factory/factory.js +67 -0
  29. package/dist/lib/factory/factory.js.map +1 -0
  30. package/dist/public-api.js +21 -0
  31. package/dist/public-api.js.map +1 -0
  32. package/package.json +31 -0
  33. package/src/lib/config/link-config.ts +28 -0
  34. package/src/lib/database/database.ts +43 -0
  35. package/src/lib/database/mssql.ts +47 -0
  36. package/src/lib/database/mssql2.ts +57 -0
  37. package/src/lib/database/prepare-call.ts +24 -0
  38. package/src/lib/database/prepared-stmt.ts +65 -0
  39. package/src/lib/database/schema/common-schema.ts +77 -0
  40. package/src/lib/database/schema/common-service.ts +204 -0
  41. package/src/lib/database/schema/table-catelog.ts +70 -0
  42. package/src/lib/database/transaction.ts +12 -0
  43. package/src/lib/dispatcher/health-service.ts +83 -0
  44. package/src/lib/dispatcher/i-dispatcher.ts +165 -0
  45. package/src/lib/dispatcher/table-catelog.ts +70 -0
  46. package/src/lib/factory/factory.ts +14 -0
  47. package/src/public-api.ts +5 -0
@@ -0,0 +1,65 @@
1
+ export class PreparedStmt {
2
+ constructor(private stmt:any, private query:string, private provider:any){
3
+ }
4
+ async execute(input:any, types?:any){
5
+ try{
6
+ let logQuery:any = this.query;
7
+ Object.keys(input).forEach((key:string)=>{
8
+ let dataType = this.findDataType(input[key]);
9
+ if(!['attributes'].includes(key) && this.query.indexOf(`@${key}`) > -1 && dataType != -1){
10
+ if(dataType === -2){
11
+ var paramsObj = input[key].reduce((obj, val, idx) => {
12
+ obj[`id${idx}`] = val;
13
+ input[`id${idx}`] = val;
14
+ this.stmt.input(`id${idx}`, this.provider.VarChar(200));
15
+ return obj;
16
+ }, {});
17
+ let inQuery = `${Object.keys(paramsObj).map((o) => {return `@${o}`}).join(',')}`;
18
+ let sQuery:any = this.query;
19
+ this.query = sQuery.replaceAll(`@${key}`, inQuery);
20
+ //logQuery = this.query;
21
+ }else
22
+ this.stmt.input(key, dataType);
23
+ new Promise((resolve, reject)=>{
24
+ let regEx = new RegExp(`@${key}`, "g")
25
+ logQuery = logQuery.replaceAll(regEx, `'${input[key]}'`);
26
+ });
27
+ }
28
+ });
29
+ new Promise((resolve, reject)=>{
30
+ console.log(logQuery);
31
+ });
32
+ await this.stmt.prepare(this.query);
33
+ let resultset = await this.stmt.execute(input);
34
+ return resultset.recordset;
35
+ }catch(err){
36
+ console.log(err.toString());
37
+ if(err.originalError?.errors){
38
+ err.originalError?.errors.forEach((item:any)=>{
39
+ console.log(item.toString());
40
+ throw new Error(item);
41
+ });
42
+ }
43
+ throw err;
44
+ }finally{
45
+ try{
46
+ await this.stmt.unprepare();
47
+ }catch(iErr){
48
+ //Do Nothing
49
+ }
50
+ }
51
+ }
52
+ findDataType(data:any){
53
+ let resultType = this.provider.VarChar;
54
+ if(data instanceof Number){
55
+ resultType = this.provider.Decimal
56
+ }else if(typeof data === 'number'){
57
+ resultType = this.provider.Decimal
58
+ }else if(data instanceof Array){
59
+ resultType = -2;
60
+ }else if(data != null && typeof data === 'object'){
61
+ resultType = -1;
62
+ }
63
+ return resultType;
64
+ }
65
+ }
@@ -0,0 +1,77 @@
1
+ export const CommonSchema = [
2
+ {
3
+ id: 'FIND_FIN_YR',
4
+ Query: `SELECT FinYear FIND_FIN_YR FROM Mst_AccYear WHERE OpenStatus='O' AND CurrentFinancialYear='Y'`
5
+ },
6
+ {
7
+ id: 'USP_GENERATE_DOCNO',
8
+ Query: `DECLARE @RESULT VARCHAR(100);EXEC USP_GENERATECOUNT @DOCREFNO, @CLNGCODE, @RESULT OUTPUT SELECT @RESULT RESULT`
9
+ },
10
+ {
11
+ id: 'ADD_NOTIFY',
12
+ Query: `insert into TM_NOTIFICATIONS(CLNORGCODE, TRNUMBER, NOTIFICATION, READSTATUS, STATUS, CREATED_ON, MOBILE) values(@clnorgcode, @trnumber, @message,'0','0', getdate(), @mobile)`
13
+ },
14
+ {
15
+ id: 'USP_GENERATE_DOCNO_RESULT',
16
+ Query: `DECLARE @RESULT VARCHAR(100);EXEC USP_GENERATECOUNT @DOCREFNO, @CLNGCODE, @RESULT OUTPUT SELECT @RESULT RESULT`
17
+ },
18
+ {
19
+ id: 'sendSMSTransport',
20
+ url: `https://sms.zestwings.com/smpp.sms?`,
21
+ },
22
+ {
23
+ id: 'getStoredProcDetails',
24
+ Query: `SELECT SUBSTRING(p.name, 2, LEN(p.name)) AS ParameterName, t.name AS ParameterType, p.max_length AS ParameterLength, p.is_output
25
+ FROM sys.parameters AS p
26
+ JOIN sys.types AS t ON t.user_type_id = p.user_type_id
27
+ WHERE object_id = OBJECT_ID(@table)`
28
+ },
29
+ {
30
+ id: 'getTableDetails',
31
+ Query: `select cl.COLUMN_NAME, cl.DATA_TYPE,
32
+ CASE
33
+ WHEN COLUMNPROPERTY(object_id(TABLE_SCHEMA+'.'+TABLE_NAME), cl.COLUMN_NAME, 'IsIdentity') = 1
34
+ THEN 1
35
+ ELSE 0
36
+ END AS IS_IDENTITY_KEY,
37
+ ISNULL(cu.IS_PRIMARY_KEY, 0) IS_PRIMARY_KEY
38
+ from INFORMATION_SCHEMA.COLUMNS cl
39
+ left join (select C.COLUMN_NAME, 1 as IS_PRIMARY_KEY FROM
40
+ INFORMATION_SCHEMA.TABLE_CONSTRAINTS T
41
+ JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE C
42
+ ON C.CONSTRAINT_NAME=T.CONSTRAINT_NAME
43
+ WHERE
44
+ C.TABLE_NAME=@table
45
+ and T.CONSTRAINT_TYPE='PRIMARY KEY') cu on cu.COLUMN_NAME = cl.COLUMN_NAME
46
+ where TABLE_NAME = @table`,
47
+ //postProcessor: CommonService.addPrimaryKeys.bind(CommonService)
48
+ },
49
+ /*{
50
+ id: 'getTableData',
51
+ executor: CommonService.getTableData.bind(CommonService)
52
+ }*/,
53
+ /*{
54
+ id: 'insertTableData',
55
+ executor: CommonService.insertTableData.bind(CommonService)
56
+ }*/,
57
+ /*{
58
+ id: 'updateTableData',
59
+ executor: CommonService.updateTableData.bind(CommonService)
60
+ },
61
+ {
62
+ id: 'deleteTableData',
63
+ executor: CommonService.deleteTableData.bind(CommonService)
64
+ }*/,
65
+ {
66
+ id: 'status-filter',
67
+ filter: `STATUS='A'`
68
+ },
69
+ {
70
+ id: 'rec-status-filter',
71
+ filter: `REC_STATUS='A'`
72
+ },
73
+ {
74
+ id: 'hims-filter',
75
+ filter: `CLNORGCODE = @Hospital_Id and STATUS='A'`
76
+ }
77
+ ]
@@ -0,0 +1,204 @@
1
+ import { CommonSchema } from "./common-schema";
2
+
3
+ import { Schema, PostProcessor, Executor } from "tharak-api";
4
+ import { TableCatelog } from "./table-catelog";
5
+ @Schema({
6
+ id: 'common',
7
+ table: CommonSchema
8
+ })
9
+ export class CommonService {
10
+ static async fileStore(input, results){
11
+
12
+ }
13
+ static async transVisits(input, results){
14
+ results.filter((item:any)=>{
15
+ item.masterData = ['Assigned', 'UnAssigned'];
16
+ if(item.ASSIGNED_STATUS === 'Assigned'){
17
+ item.masterData.push('Start');
18
+ }
19
+ })
20
+ return results;
21
+ }
22
+ static async exStoredProcData(input, table, trans, provider){
23
+
24
+ }
25
+ static async getQueryData(input, table, trans, provider){
26
+ let tableQuery = {Query: input.Query}
27
+ return await provider.executeQuery(input, tableQuery, trans);
28
+ }
29
+ private countWords(str) {
30
+ return str.trim().split(/\s+/).length;
31
+ }
32
+ private createFilter(input){
33
+ let filter=''
34
+ Object.keys(input).forEach((key, index)=>{
35
+ if(!['attributes', 'table', 'NOFILTER', 'fetchId'].includes(key)){
36
+ if(index > 0){
37
+ filter += ` and `
38
+ }
39
+ filter += `${key}=@${key}`
40
+ }
41
+ })
42
+ if(filter)
43
+ return {filter: filter};
44
+ }
45
+ @Executor('getTableData')
46
+ async getTableData(input, table, trans, provider){
47
+ let context = input.attributes['context'];
48
+ let tableQuery;
49
+ if(input.fetchId){
50
+ tableQuery = table.find((item:any)=>item.id === input.fetchId);
51
+ }
52
+ if(!tableQuery && this.countWords(input.table) === 1){
53
+ let selects = input.selects || '*';
54
+ let basicQuery = `select TOP (1000) ${selects.toString()} from ${input.table} `;
55
+ let filter = table.find((item:any)=>item?.id === input.filterId);
56
+ if(!filter)
57
+ filter = table.find((item:any)=>item?.id === 'status-filter' && !input.NOFILTER);
58
+ if(!filter){
59
+ filter = this.createFilter(input);
60
+ }
61
+ if(filter)
62
+ basicQuery += ` where ${filter.filter}`;
63
+ let tTable = table.find((item:any)=>item?.id === 'getTableData');
64
+ if(tTable){
65
+ tableQuery = tTable;
66
+ tableQuery.Query = basicQuery;
67
+ }else
68
+ tableQuery = {Query: basicQuery}
69
+ }
70
+ for(let ctxtKey in context){
71
+ if(!input[ctxtKey])
72
+ input[ctxtKey] = context[ctxtKey];
73
+ }
74
+ if(tableQuery){
75
+ //delete input.attributes;
76
+ return await provider.executeQuery(input, tableQuery, trans);
77
+ }else{
78
+ throw new Error('No Query Found');
79
+ }
80
+ }
81
+ @Executor('insertTableData')
82
+ async insertTableData(input, table, trans, provider){
83
+ let tableName = input.table;
84
+ let index = 0;
85
+ let tokens = '';
86
+ let elems = ''
87
+ let attributes = input.attributes;
88
+ delete input['attributes'];
89
+ delete input['table'];
90
+ delete input['files'];
91
+ for(let item in input){
92
+ if(['attributes','table', 'files', ''].includes(item))
93
+ continue;
94
+ if(!input[item] && attributes && attributes['context']){
95
+ input[item] = attributes['context'][item];
96
+ }
97
+ if(!input[item])
98
+ continue;
99
+ if(index !== 0){
100
+ tokens += ',';
101
+ elems += ',';
102
+ }
103
+ tokens += `@${item}`;
104
+ elems += `${item}`;
105
+ index++;
106
+
107
+ }
108
+ if(this.countWords(tableName) === 1){
109
+ let basicQuery = `insert into ${tableName} (${elems}) values (${tokens})`;
110
+ let tableQuery = {Query: basicQuery}
111
+ return await provider.executeQuery(input, tableQuery, trans);
112
+ }
113
+ }
114
+ @Executor('updateTableData')
115
+ async updateTableData(input, table, trans, provider){
116
+ let tableName = input.table;
117
+ let index = 0;
118
+ let tokens = '';
119
+ delete input['attributes'];
120
+ delete input['table'];
121
+ delete input['files'];
122
+ //delete input['filter'];
123
+ if(this.countWords(tableName) === 1){
124
+ for(let item in input){
125
+ if(['attributes','table', 'files', 'filter', 'FILTER_CLOUMNS'].includes(item))
126
+ continue;
127
+ /*if(input.FILTER_CLOUMNS.includes(item)){
128
+ continue;
129
+ }*/
130
+ if(index !== 0){
131
+ tokens += ',';
132
+ }
133
+ tokens += `${item}=@${item}`;
134
+ index++;
135
+ }
136
+ let filter = '';
137
+ let basicQuery = `update ${tableName} set ${tokens}`;
138
+ if(input.filterId){
139
+ filter = table.find((item:any)=>item.id === input.filterId);
140
+ }else{
141
+ filter = this.findFilter(tableName, input);
142
+ }
143
+ basicQuery += ` where ${filter}`;
144
+ let tableQuery = {Query: basicQuery}
145
+ return await provider.executeQuery(input, tableQuery, trans);
146
+ }
147
+ }
148
+ @Executor('deleteTableData')
149
+ async deleteTableData(input, table, trans, provider){
150
+ for(let ctxtKey in input.attributes['context']){
151
+ input[ctxtKey] = input.attributes['context'][ctxtKey];
152
+ }
153
+ let tableName = input.table;
154
+ if(this.countWords(input.table) === 1){
155
+ let basicQuery = `delete from ${tableName}`;
156
+ let filter = this.findFilter(tableName, input);
157
+ if(filter){
158
+ basicQuery += ` where ${filter}`;
159
+ }
160
+ let tableQuery = {Query: basicQuery}
161
+ return await provider.executeQuery(input, tableQuery, trans);
162
+ }
163
+ }
164
+ findFilter(tableName:string, input:any){
165
+ let filter = ''
166
+ if(input.filterId){
167
+
168
+ }
169
+ let pKeys:any = this.findPrimaryKeys(tableName);
170
+ pKeys.forEach((col, index)=>{
171
+ if(index > 0){
172
+ filter += ` and `
173
+ }
174
+ filter += `${col.COLUMN_NAME}=@${col.COLUMN_NAME}`
175
+ })
176
+ return filter;
177
+ }
178
+ findPrimaryKeys(tableName){
179
+ let table:any = TableCatelog.find((table:any)=>table.id === tableName);
180
+ if(table && table.primaryKeys?.length > 0){
181
+ return table.primaryKeys;
182
+ }
183
+ return [];
184
+ }
185
+ @PostProcessor('getTableData')
186
+ async addPrimaryKeys(input, results){
187
+ let table:any = TableCatelog.find((table:any)=>table.id === input.table);
188
+ if(table && table.primaryKeys?.length > 0){
189
+ for(let primaryKey of table.primaryKeys){
190
+ let found = results.find((col:any)=>col.COLUMN_NAME === primaryKey.COLUMN_NAME);
191
+ if(found){
192
+ found.IS_PRIMARY_KEY = 1;
193
+ }
194
+ }
195
+ }
196
+ return results;
197
+ }
198
+ @PostProcessor('getTableData')
199
+ async postGetData(input, results){
200
+ console.log('Hi')
201
+ //tableQuery = table.find((item:any)=>item.id === input.fetchId);
202
+ return results;
203
+ }
204
+ }
@@ -0,0 +1,70 @@
1
+ export const TableCatelog = [
2
+ {
3
+ id: 'MST_SERVGROUPS',
4
+ primaryKeys: [
5
+ {COLUMN_NAME: 'SRVGRPCODE'}
6
+ ],
7
+ },
8
+ {
9
+ id: 'MST_SERVSUBGRP',
10
+ primaryKeys: [
11
+ {COLUMN_NAME: 'SUBGRPCODE'}
12
+ ],
13
+ },
14
+ {
15
+ id: 'Mst_ChargeSheet',
16
+ primaryKeys: [
17
+ {COLUMN_NAME: 'TARIFFID'},
18
+ {COLUMN_NAME: 'CONS_TYPE'},
19
+ {COLUMN_NAME: 'Doctor_ID'}
20
+ ],
21
+ },
22
+ {
23
+ id: 'HospitalsList',
24
+ primaryKeys: [
25
+ {COLUMN_NAME: 'Hospital_Id'},
26
+ ],
27
+ },
28
+ {
29
+ id: 'MST_REFERRAL',
30
+ primaryKeys: [
31
+ {COLUMN_NAME: 'REFERRAL_ID'},
32
+ ],
33
+ },
34
+ {
35
+ id: 'SECURITY_USERS',
36
+ primaryKeys: [
37
+ {COLUMN_NAME: 'USERID'},
38
+ ],
39
+ },
40
+ {
41
+ id: 'WL_EMP_MAST',
42
+ primaryKeys: [
43
+ {COLUMN_NAME: 'MOBILE'},
44
+ ],
45
+ },
46
+ {
47
+ id: 'TM_PATIENTPREFERENCES',
48
+ primaryKeys: [
49
+ {COLUMN_NAME: 'USERID'},
50
+ ],
51
+ },
52
+ {
53
+ id: 'MST_REFERRALSOURCE',
54
+ primaryKeys: [
55
+ {COLUMN_NAME: 'SOURCE_ID'},
56
+ ],
57
+ },
58
+ {
59
+ id: 'Mst_DoctorMaster',
60
+ primaryKeys: [
61
+ {COLUMN_NAME: 'Code'},
62
+ ],
63
+ },
64
+ {
65
+ id: 'Speciality_Master',
66
+ primaryKeys: [
67
+ {COLUMN_NAME: 'Speciality_ID'},
68
+ ],
69
+ },
70
+ ];
@@ -0,0 +1,12 @@
1
+ export class Transaction {
2
+ constructor(private transact:any){
3
+ }
4
+ commit(){
5
+ if(!this.transact._aborted)
6
+ return this.transact.commit();
7
+ }
8
+ rollback(){
9
+ if(!this.transact._aborted)
10
+ return this.transact.rollback();
11
+ }
12
+ }
@@ -0,0 +1,83 @@
1
+ import { Api, GET, POST, UnSecured } from "tharak-api";
2
+ import { IDispatcher } from "./i-dispatcher";
3
+ import { Database } from "../database/database";
4
+
5
+ @Api('/')
6
+ export class HealthService extends IDispatcher{
7
+ @POST('/:service/:operation')
8
+ async postData(request, response){
9
+ let resultset: any;
10
+ let params = request.params;
11
+ let input: any = request.body;
12
+ let files:any = request?.files;
13
+ if(files && !Array.isArray(files)){
14
+ input.files = [files.file];
15
+ }else{
16
+ input.files = files;
17
+ }
18
+ input.attributes = request.attributes;
19
+ input.method = request.method;
20
+ let trans:any = await Database.findTransaction();
21
+ try{
22
+ resultset = await this.execute(params, input, trans);
23
+ response.send(resultset);
24
+ trans.commit();
25
+ }catch(err){
26
+ trans.rollback();
27
+ response.status(500).send({ status: 1, message: err.message });
28
+ }
29
+ }
30
+ @GET('/i18n/:operation')
31
+ @UnSecured()
32
+ async loadi18nData(request, response){
33
+ request.params.service = 'i18n';
34
+ await this.getQueryData(request, response);
35
+ }
36
+ @GET('/customer/:operation')
37
+ @UnSecured()
38
+ async loadCustData(request, response){
39
+ request.params.service = 'customer';
40
+ await this.getQueryData(request, response);
41
+ }
42
+ @GET('/master/getCountryCodes')
43
+ @UnSecured()
44
+ async findUnsecureCountry(request, response){
45
+ request.params = {
46
+ operation: "getCountryCodes",
47
+ service: "master"
48
+ }
49
+ await this.getQueryData(request, response);
50
+ }
51
+ @GET('/registration/checkPatientByMobile')
52
+ @UnSecured()
53
+ async checkPatientByMobile(request, response){
54
+ request.params = {
55
+ operation: "checkPatientByMobile",
56
+ service: "registration"
57
+ }
58
+ await this.getQueryData(request, response);
59
+ }
60
+ @GET('/registration/genOTPByMobile')
61
+ @UnSecured()
62
+ async genOTPByMobile(request, response){
63
+ request.params = {
64
+ operation: "genOTPByMobile",
65
+ service: "registration"
66
+ }
67
+ await this.getQueryData(request, response);
68
+ }
69
+ @GET('/:service/:operation')
70
+ async getQueryData(request, response){
71
+ let params = request.params;
72
+ let input: any = request.query;
73
+ let resultset: any;
74
+ input.attributes = request.attributes;
75
+ try{
76
+ resultset = await this.execute(params, input, undefined);
77
+ response.send(resultset);
78
+ }catch(err){
79
+ console.log(err.toString())
80
+ response.status(500).send({ status: 1, message: err.message });
81
+ }
82
+ }
83
+ }
@@ -0,0 +1,165 @@
1
+
2
+ import { Database } from "../database/database";
3
+
4
+ export abstract class IDispatcher {
5
+ getSchemaConfig(){
6
+ return null;
7
+ };
8
+ async findSchema(params:any){
9
+ let schema:any = this.getSchemaConfig().find((item:any)=>item.id === params.service);
10
+ if(!schema){
11
+ throw new Error('Schema not found.');
12
+ }
13
+ schema.table = schema.table || [];
14
+ if(!schema.isLoaded){
15
+ let common:any = this.getSchemaConfig().find((item:any)=>item.id === 'common');
16
+ schema.table = schema.table.concat(common.table);
17
+ schema.isLoaded = true;
18
+ }
19
+ return schema;
20
+ }
21
+ async findTable(params:any, schema:any){
22
+ let table = schema.table.find((item:any)=>item?.id === params.operation);
23
+
24
+ return table;
25
+ }
26
+ async execute(params, input, trans){
27
+ let result;
28
+ let resultset: any = [];
29
+ let schema = await this.findSchema(params);
30
+ let table = await this.findTable(params, schema);
31
+
32
+ if(table?.executor){
33
+ resultset = await table.executor(input, schema.table, trans, this);
34
+ result = { status: 0, results: resultset };
35
+ }else if(table?.executorSet){
36
+ resultset = await this.executeSet(input, table, trans);
37
+ result = { status: 0, results: resultset };
38
+ }else if(table?.url){
39
+ resultset = await this.executeUrl(input, table, trans);
40
+ result = { status: 0, results: resultset };
41
+ }else if(table?.Query){
42
+ resultset = await this.executeQuery(input, table, trans);
43
+ result = { status: 0, results: resultset };
44
+ }else if(table?.results){
45
+ resultset = table?.results;
46
+ result = { status: 0, results: resultset };
47
+ }else if(table?.data){
48
+ result = table.data
49
+ }else{
50
+ throw new Error('Query not found.');
51
+ }
52
+ return result;
53
+ }
54
+ async executeSet(input:any, table:any, trans:any){
55
+ for(let executorId of table.executorSet){
56
+ let executor = table.find(
57
+ (item: any) => item.id === executorId
58
+ );
59
+ let uspGenDocNo = await this.executeQuery(
60
+ input,
61
+ executor,
62
+ trans
63
+ );
64
+ input.appNo = uspGenDocNo[0]["RESULT"];
65
+ }
66
+ }
67
+ async executeUrl(input:any, table:any, trans:any){
68
+ let result;
69
+ input.headers = {
70
+ 'Content-type': 'application/json; charset=UTF-8'
71
+ };
72
+ if(table.preProcessor){
73
+ await table.preProcessor(input, []);
74
+ }
75
+ if(input.method === 'POST'){
76
+ let req:any = {
77
+ method: 'POST',
78
+ headers: input.headers
79
+ };
80
+ if(input.headers){
81
+ delete input.headers;
82
+ }
83
+ if(input.files){
84
+ delete input.files;
85
+ }
86
+ delete input.attributes;
87
+ req.body = JSON.stringify(input);
88
+ result = await fetch(table?.url, req)
89
+ }else{
90
+ let req = {
91
+ headers: input.headers
92
+ };
93
+ let params = this.convert2Url(input);
94
+ result = await fetch(`${table?.url}${params}`, req)
95
+ }
96
+ let posts = await result.json();
97
+ if(table.postProcessor){
98
+ posts = await table.postProcessor(input, posts);
99
+ }
100
+ return posts;
101
+ }
102
+
103
+ async executeProcedure(input:any, output:any, table:any, trans:any){
104
+ let stmt = await Database.preparedCall(table.Query, trans);
105
+ return stmt.execute(input, output);
106
+ }
107
+ async executeQuery(input:any, table:any, trans?:any){
108
+ let resultset: any = [];
109
+ try{
110
+ let stmt;
111
+ if(trans)
112
+ stmt = await Database.preparedStatement(table.Query, trans);
113
+ else
114
+ stmt = await Database.preparedStatement(table.Query);
115
+ this.parseParams(input, table);
116
+ if(table.preProcessors){
117
+ for(let processor of table.preProcessors){
118
+ resultset = await processor(input, resultset);
119
+ }
120
+ }
121
+ resultset = await stmt.execute(input);
122
+ if(table.postProcessors){
123
+ for(let processor of table.postProcessors){
124
+ let pResult = await processor(input, resultset);
125
+ if(pResult){
126
+ resultset = pResult;
127
+ }
128
+ }
129
+ }
130
+ }catch(err){
131
+ console.log(err.toString());
132
+ throw new Error(err.message);
133
+ }
134
+ return resultset;
135
+ }
136
+ parseParams(input:any, table:any){
137
+ if(table.params && table.params.length > 0){
138
+ table.params.forEach((param:any)=>{
139
+ if(param.opr.toLowerCase() === 'in'){
140
+ input[param.name] = input[param.name].split(",")//JSON.parse(input[param.name]);
141
+ }
142
+ if(param.opr === 'like'){
143
+ input[param.name] = `%${input[param.name]}%`;
144
+ }
145
+ if(param.opr === 'decode'){
146
+ input[param.name] = `${btoa(input[param.name])}`;
147
+ }
148
+ if(param.opr === 'default'){
149
+ input[param.name] = `${input[param.name] || param.value}`;
150
+ }
151
+ })
152
+ }
153
+ }
154
+ convert2Url(input){
155
+ let elements = '';
156
+ let idx=0;
157
+ for(let [key, value] of Object.entries(input)){
158
+ if(idx > 0)
159
+ elements += '&';
160
+ elements += key+'='+value;
161
+ idx++;
162
+ }
163
+ return elements;
164
+ }
165
+ }