@futdevpro/nts-dynamo 1.2.90
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/BuildPackage.bat +1 -0
- package/LICENSE +21 -0
- package/README.md +17 -0
- package/lib/_constants/dynamobe-global.settings.d.ts +2 -0
- package/lib/_constants/dynamobe-global.settings.d.ts.map +1 -0
- package/lib/_constants/dynamobe-global.settings.js +11 -0
- package/lib/_enum/data-model-types.enum.d.ts +5 -0
- package/lib/_enum/data-model-types.enum.d.ts.map +1 -0
- package/lib/_enum/data-model-types.enum.js +9 -0
- package/lib/_enum/dynamobe-data-service-function.enum.d.ts +7 -0
- package/lib/_enum/dynamobe-data-service-function.enum.d.ts.map +1 -0
- package/lib/_enum/dynamobe-data-service-function.enum.js +14 -0
- package/lib/_enum/dynamobe-route-security.enum.d.ts +5 -0
- package/lib/_enum/dynamobe-route-security.enum.d.ts.map +1 -0
- package/lib/_enum/dynamobe-route-security.enum.js +9 -0
- package/lib/_enum/http/http-call-type.enum.d.ts +7 -0
- package/lib/_enum/http/http-call-type.enum.d.ts.map +1 -0
- package/lib/_enum/http/http-call-type.enum.js +11 -0
- package/lib/_enum/http/http-response-type.enum.d.ts +6 -0
- package/lib/_enum/http/http-response-type.enum.d.ts.map +1 -0
- package/lib/_enum/http/http-response-type.enum.js +10 -0
- package/lib/_enum/log-style.enum.d.ts +25 -0
- package/lib/_enum/log-style.enum.d.ts.map +1 -0
- package/lib/_enum/log-style.enum.js +29 -0
- package/lib/_enum/predefined-data-types.enum.d.ts +7 -0
- package/lib/_enum/predefined-data-types.enum.d.ts.map +1 -0
- package/lib/_enum/predefined-data-types.enum.js +11 -0
- package/lib/_models/dynamobe-api-call-params.d.ts +34 -0
- package/lib/_models/dynamobe-api-call-params.d.ts.map +1 -0
- package/lib/_models/dynamobe-api-call-params.js +24 -0
- package/lib/_models/dynamobe-app-params.d.ts +14 -0
- package/lib/_models/dynamobe-app-params.d.ts.map +1 -0
- package/lib/_models/dynamobe-app-params.js +13 -0
- package/lib/_models/dynamobe-data-model-params.d.ts +12 -0
- package/lib/_models/dynamobe-data-model-params.d.ts.map +1 -0
- package/lib/_models/dynamobe-data-model-params.js +14 -0
- package/lib/_models/dynamobe-endpoint-control.d.ts +28 -0
- package/lib/_models/dynamobe-endpoint-control.d.ts.map +1 -0
- package/lib/_models/dynamobe-endpoint-control.js +85 -0
- package/lib/_models/dynamobe-global-settings.d.ts +8 -0
- package/lib/_models/dynamobe-global-settings.d.ts.map +1 -0
- package/lib/_models/dynamobe-global-settings.js +6 -0
- package/lib/_models/fullstack-models/control-models/daily-usage-data.d.ts +17 -0
- package/lib/_models/fullstack-models/control-models/daily-usage-data.d.ts.map +1 -0
- package/lib/_models/fullstack-models/control-models/daily-usage-data.js +21 -0
- package/lib/_models/fullstack-models/control-models/dynamobe-data-property-params.d.ts +27 -0
- package/lib/_models/fullstack-models/control-models/dynamobe-data-property-params.d.ts.map +1 -0
- package/lib/_models/fullstack-models/control-models/dynamobe-data-property-params.js +66 -0
- package/lib/_models/fullstack-models/control-models/dynamobe-error.d.ts +17 -0
- package/lib/_models/fullstack-models/control-models/dynamobe-error.d.ts.map +1 -0
- package/lib/_models/fullstack-models/control-models/dynamobe-error.js +195 -0
- package/lib/_models/fullstack-models/control-models/geo-ip-location.d.ts +25 -0
- package/lib/_models/fullstack-models/control-models/geo-ip-location.d.ts.map +1 -0
- package/lib/_models/fullstack-models/control-models/geo-ip-location.js +2 -0
- package/lib/_models/fullstack-models/control-models/usage-action.d.ts +8 -0
- package/lib/_models/fullstack-models/control-models/usage-action.d.ts.map +1 -0
- package/lib/_models/fullstack-models/control-models/usage-action.js +12 -0
- package/lib/_models/fullstack-models/control-models/usage-data.d.ts +13 -0
- package/lib/_models/fullstack-models/control-models/usage-data.d.ts.map +1 -0
- package/lib/_models/fullstack-models/control-models/usage-data.js +15 -0
- package/lib/_models/fullstack-models/data-models/custom-data.d.ts +15 -0
- package/lib/_models/fullstack-models/data-models/custom-data.d.ts.map +1 -0
- package/lib/_models/fullstack-models/data-models/custom-data.js +23 -0
- package/lib/_models/fullstack-models/data-models/metadata.d.ts +16 -0
- package/lib/_models/fullstack-models/data-models/metadata.d.ts.map +1 -0
- package/lib/_models/fullstack-models/data-models/metadata.js +16 -0
- package/lib/_models/fullstack-models/data-models/usage-session.d.ts +33 -0
- package/lib/_models/fullstack-models/data-models/usage-session.d.ts.map +1 -0
- package/lib/_models/fullstack-models/data-models/usage-session.js +48 -0
- package/lib/_modules/test-route/custom-data.service.d.ts +5 -0
- package/lib/_modules/test-route/custom-data.service.d.ts.map +1 -0
- package/lib/_modules/test-route/custom-data.service.js +12 -0
- package/lib/_modules/test-route/test-controller.d.ts +5 -0
- package/lib/_modules/test-route/test-controller.d.ts.map +1 -0
- package/lib/_modules/test-route/test-controller.js +125 -0
- package/lib/_modules/usage-route/usage-controller.d.ts +6 -0
- package/lib/_modules/usage-route/usage-controller.d.ts.map +1 -0
- package/lib/_modules/usage-route/usage-controller.js +88 -0
- package/lib/_modules/usage-route/usage.service.d.ts +18 -0
- package/lib/_modules/usage-route/usage.service.d.ts.map +1 -0
- package/lib/_modules/usage-route/usage.service.js +110 -0
- package/lib/_services/dynamobe-api.service.d.ts +9 -0
- package/lib/_services/dynamobe-api.service.d.ts.map +1 -0
- package/lib/_services/dynamobe-api.service.js +130 -0
- package/lib/_services/dynamobe-app.d.ts +11 -0
- package/lib/_services/dynamobe-app.d.ts.map +1 -0
- package/lib/_services/dynamobe-app.js +38 -0
- package/lib/_services/dynamobe-auth.service.d.ts +25 -0
- package/lib/_services/dynamobe-auth.service.d.ts.map +1 -0
- package/lib/_services/dynamobe-auth.service.js +24 -0
- package/lib/_services/dynamobe-controller.service.d.ts +10 -0
- package/lib/_services/dynamobe-controller.service.d.ts.map +1 -0
- package/lib/_services/dynamobe-controller.service.js +11 -0
- package/lib/_services/dynamobe-data.service.d.ts +51 -0
- package/lib/_services/dynamobe-data.service.d.ts.map +1 -0
- package/lib/_services/dynamobe-data.service.js +224 -0
- package/lib/_services/dynamobe-db-service-collection.service.d.ts +5 -0
- package/lib/_services/dynamobe-db-service-collection.service.d.ts.map +1 -0
- package/lib/_services/dynamobe-db-service-collection.service.js +7 -0
- package/lib/_services/dynamobe-db.service.d.ts +150 -0
- package/lib/_services/dynamobe-db.service.d.ts.map +1 -0
- package/lib/_services/dynamobe-db.service.js +577 -0
- package/lib/_services/dynamobe-email-service-collection.service.d.ts +5 -0
- package/lib/_services/dynamobe-email-service-collection.service.d.ts.map +1 -0
- package/lib/_services/dynamobe-email-service-collection.service.js +7 -0
- package/lib/_services/dynamobe-email.service.d.ts +32 -0
- package/lib/_services/dynamobe-email.service.d.ts.map +1 -0
- package/lib/_services/dynamobe-email.service.js +144 -0
- package/lib/_services/dynamobe-global.service.d.ts +22 -0
- package/lib/_services/dynamobe-global.service.d.ts.map +1 -0
- package/lib/_services/dynamobe-global.service.js +84 -0
- package/lib/_services/dynamobe-routing-module.service.d.ts +25 -0
- package/lib/_services/dynamobe-routing-module.service.d.ts.map +1 -0
- package/lib/_services/dynamobe-routing-module.service.js +112 -0
- package/lib/_services/dynamobe-server.d.ts +31 -0
- package/lib/_services/dynamobe-server.d.ts.map +1 -0
- package/lib/_services/dynamobe-server.js +179 -0
- package/lib/_services/dynamobe-shared.service.d.ts +100 -0
- package/lib/_services/dynamobe-shared.service.d.ts.map +1 -0
- package/lib/_services/dynamobe-shared.service.js +294 -0
- package/lib/_services/dynamobe-singleton.service.d.ts +5 -0
- package/lib/_services/dynamobe-singleton.service.d.ts.map +1 -0
- package/lib/_services/dynamobe-singleton.service.js +15 -0
- package/lib/index-newo.d.ts +0 -0
- package/lib/index-newo.d.ts.map +1 -0
- package/lib/index-newo.js +71 -0
- package/lib/index-old.d.ts +39 -0
- package/lib/index-old.js +49 -0
- package/lib/index.d.ts +39 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +49 -0
- package/lib/tsconfig.tsbuildinfo +2080 -0
- package/nodemon.json +6 -0
- package/package.json +52 -0
- package/src/_constants/dynamobe-global.settings.ts +11 -0
- package/src/_enum/data-model-types.enum.ts +6 -0
- package/src/_enum/dynamobe-data-service-function.enum.ts +11 -0
- package/src/_enum/dynamobe-route-security.enum.ts +6 -0
- package/src/_enum/http/http-call-type.enum.ts +8 -0
- package/src/_enum/http/http-response-type.enum.ts +7 -0
- package/src/_enum/log-style.enum.ts +30 -0
- package/src/_enum/predefined-data-types.enum.ts +8 -0
- package/src/_models/dynamobe-api-call-params.ts +62 -0
- package/src/_models/dynamobe-app-params.ts +24 -0
- package/src/_models/dynamobe-data-model-params.ts +23 -0
- package/src/_models/dynamobe-endpoint-control.ts +127 -0
- package/src/_models/dynamobe-global-settings.ts +9 -0
- package/src/_models/fullstack-models/control-models/daily-usage-data.ts +34 -0
- package/src/_models/fullstack-models/control-models/dynamobe-data-property-params.ts +98 -0
- package/src/_models/fullstack-models/control-models/dynamobe-error.ts +207 -0
- package/src/_models/fullstack-models/control-models/geo-ip-location.ts +26 -0
- package/src/_models/fullstack-models/control-models/usage-action.ts +18 -0
- package/src/_models/fullstack-models/control-models/usage-data.ts +26 -0
- package/src/_models/fullstack-models/data-models/custom-data.ts +37 -0
- package/src/_models/fullstack-models/data-models/metadata.ts +28 -0
- package/src/_models/fullstack-models/data-models/usage-session.ts +82 -0
- package/src/_modules/test-route/custom-data.service.ts +21 -0
- package/src/_modules/test-route/test-controller.ts +136 -0
- package/src/_modules/usage-route/usage-controller.ts +114 -0
- package/src/_modules/usage-route/usage.service.ts +125 -0
- package/src/_services/dynamobe-api.service.ts +135 -0
- package/src/_services/dynamobe-app.ts +49 -0
- package/src/_services/dynamobe-auth.service.ts +42 -0
- package/src/_services/dynamobe-controller.service.ts +20 -0
- package/src/_services/dynamobe-data.service.ts +215 -0
- package/src/_services/dynamobe-db-service-collection.service.ts +9 -0
- package/src/_services/dynamobe-db.service.ts +556 -0
- package/src/_services/dynamobe-email-service-collection.service.ts +9 -0
- package/src/_services/dynamobe-email.service.ts +169 -0
- package/src/_services/dynamobe-global.service.ts +99 -0
- package/src/_services/dynamobe-routing-module.service.ts +141 -0
- package/src/_services/dynamobe-server.ts +225 -0
- package/src/_services/dynamobe-shared.service.ts +320 -0
- package/src/_services/dynamobe-singleton.service.ts +16 -0
- package/src/index-newo.ts +71 -0
- package/src/index.ts +50 -0
- package/src/version.json +2 -0
- package/tsconfig.app.json +12 -0
- package/tsconfig.json +72 -0
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
|
|
2
|
+
import { DynamoBEDataService } from '../../_services/dynamobe-data.service';
|
|
3
|
+
import { DynamoBEError } from '../../_models/fullstack-models/control-models/dynamobe-error';
|
|
4
|
+
import { DBE_Shared } from '../../_services/dynamobe-shared.service';
|
|
5
|
+
import { Request } from 'express';
|
|
6
|
+
import { DynamoUsageData } from '../../_models/fullstack-models/control-models/usage-data';
|
|
7
|
+
import { DailyUsageData } from '../../_models/fullstack-models/control-models/daily-usage-data';
|
|
8
|
+
import { DynamoUsageSession, usageSessionModelParams } from '../../_models/fullstack-models/data-models/usage-session';
|
|
9
|
+
import { DBE_Global_S } from '../../_services/dynamobe-global.service';
|
|
10
|
+
|
|
11
|
+
export class UsageService extends DynamoBEDataService<DynamoUsageSession> {
|
|
12
|
+
usageData: DynamoUsageData[] = [];
|
|
13
|
+
simplifiedDailyUsage: DailyUsageData[] = [];
|
|
14
|
+
|
|
15
|
+
constructor(
|
|
16
|
+
set?: {
|
|
17
|
+
usageSession?: DynamoUsageSession,
|
|
18
|
+
usageData?: DynamoUsageData[],
|
|
19
|
+
issuer?: string,
|
|
20
|
+
}
|
|
21
|
+
) {
|
|
22
|
+
super(
|
|
23
|
+
DBE_Global_S.getDBServiceCollection().usageDataDBService,
|
|
24
|
+
new DynamoUsageSession(set?.usageSession),
|
|
25
|
+
usageSessionModelParams,
|
|
26
|
+
set?.issuer
|
|
27
|
+
);
|
|
28
|
+
|
|
29
|
+
this.usageData = set?.usageData ? set?.usageData : [];
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
getLocationDataFromRequest(req: Request): void {
|
|
33
|
+
try {
|
|
34
|
+
this.data.address = DBE_Shared.getIpFromRequest(req);
|
|
35
|
+
this.data.locationData = DBE_Shared.getLocationDataByRequest(req);
|
|
36
|
+
|
|
37
|
+
/* console.log(`...getLocationDataFromRequest... ip: ${this.data.address}`, this.data.locationData); */
|
|
38
|
+
} catch (error) {
|
|
39
|
+
throw new DynamoBEError({ status: 417, message: 'getLocationDataFromRequest was unsuccessful', error });
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
async getLastWeekUsage(): Promise<void> {
|
|
44
|
+
try {
|
|
45
|
+
await this.dataDBService.find({
|
|
46
|
+
sessionStart: { $gte: DBE_Shared.oneWeekAgo() }
|
|
47
|
+
}).then((res: DynamoUsageSession[]) => {
|
|
48
|
+
this.dataList = res;
|
|
49
|
+
});
|
|
50
|
+
this.simplifiedDailyUsage = [];
|
|
51
|
+
this.dataList.forEach((session: DynamoUsageSession) => {
|
|
52
|
+
let dateIndex = this.simplifiedDailyUsage.findIndex(
|
|
53
|
+
(dailyData: DailyUsageData) => dailyData.date === session.date
|
|
54
|
+
);
|
|
55
|
+
if (-1 < dateIndex) {
|
|
56
|
+
this.simplifiedDailyUsage[dateIndex].visitations.push(session);
|
|
57
|
+
} else {
|
|
58
|
+
const newUsageSimple = new DailyUsageData({ date: session.date, visitations: [ session ] });
|
|
59
|
+
this.simplifiedDailyUsage.push(newUsageSimple);
|
|
60
|
+
dateIndex = this.simplifiedDailyUsage.length - 1;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
if (session?.locationData?.country) {
|
|
64
|
+
if (session.locationData.country === 'HU') {
|
|
65
|
+
this.simplifiedDailyUsage[dateIndex].visitorsHun++;
|
|
66
|
+
} else {
|
|
67
|
+
this.simplifiedDailyUsage[dateIndex].visitorsElse++;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
this.simplifiedDailyUsage[dateIndex].totalVisitTime += session.totalSessionTime;
|
|
71
|
+
this.simplifiedDailyUsage[dateIndex].averageVisitTime = Math.round(
|
|
72
|
+
(this.simplifiedDailyUsage[dateIndex].totalVisitTime /
|
|
73
|
+
this.simplifiedDailyUsage[dateIndex].visitations.length) * 100
|
|
74
|
+
) / 100;
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
this.simplifiedDailyUsage.sort(
|
|
78
|
+
(a: DailyUsageData, b: DailyUsageData) => +new Date(b.date) - +new Date(a.date)
|
|
79
|
+
);
|
|
80
|
+
this.simplifiedDailyUsage.forEach((daily: DailyUsageData) => {
|
|
81
|
+
daily.visitations.sort(
|
|
82
|
+
(a: DynamoUsageSession, b: DynamoUsageSession) => +a.sessionStart - +b.sessionStart
|
|
83
|
+
);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
} catch (error) {
|
|
87
|
+
throw new DynamoBEError({ status: 417, message: 'getDailyUsage was unsuccessful', error });
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
async updateUsageData(req: Request): Promise<void> {
|
|
92
|
+
try {
|
|
93
|
+
await this.getDataById();
|
|
94
|
+
this.data.usageData = this.usageData;
|
|
95
|
+
let totalTime = 0;
|
|
96
|
+
this.data.usageData.forEach((data: DynamoUsageData) => {
|
|
97
|
+
totalTime += data.timeSpentOnPage;
|
|
98
|
+
});
|
|
99
|
+
if (!this.data.locationData) {
|
|
100
|
+
this.getLocationDataFromRequest(req);
|
|
101
|
+
}
|
|
102
|
+
this.data.totalSessionTime = totalTime;
|
|
103
|
+
await this.saveData();
|
|
104
|
+
} catch (error) {
|
|
105
|
+
throw new DynamoBEError({ status: 417, message: 'updateSessionData was unsuccessful', error });
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
async closeSession(req: Request): Promise<void> {
|
|
110
|
+
try {
|
|
111
|
+
await this.getDataById();
|
|
112
|
+
this.data.sessionEnd = new Date();
|
|
113
|
+
const newTotalSessionTime = +this.data.sessionEnd - +this.data.sessionStart;
|
|
114
|
+
this.data.usageData[this.data.usageData.length - 1].timeSpentOnPage = newTotalSessionTime - this.data.totalSessionTime;
|
|
115
|
+
if (!this.data.locationData) {
|
|
116
|
+
this.getLocationDataFromRequest(req);
|
|
117
|
+
}
|
|
118
|
+
this.data.usageData.sort((a: DynamoUsageData, b: DynamoUsageData) => +a.opened - +b.opened);
|
|
119
|
+
this.data.totalSessionTime = newTotalSessionTime;
|
|
120
|
+
await this.saveData();
|
|
121
|
+
} catch (error) {
|
|
122
|
+
throw new DynamoBEError({ status: 417, message: 'closeSession was unsuccessful', error });
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
|
|
2
|
+
import * as axios from 'axios';
|
|
3
|
+
import { HttpCallType } from '../_enum/http/http-call-type.enum';
|
|
4
|
+
import { HttpResponseType } from '../_enum/http/http-response-type.enum';
|
|
5
|
+
import { DynamoBEApiCallParams } from '../_models/dynamobe-api-call-params';
|
|
6
|
+
import { DynamoBEError } from '../_models/fullstack-models/control-models/dynamobe-error';
|
|
7
|
+
import { DBE_Shared } from './dynamobe-shared.service';
|
|
8
|
+
|
|
9
|
+
export class DynamoBEApiService {
|
|
10
|
+
|
|
11
|
+
public static async startApiCall<T>(
|
|
12
|
+
callParams: DynamoBEApiCallParams,
|
|
13
|
+
inputParams?: {
|
|
14
|
+
pathParams?: {
|
|
15
|
+
[param: string]: string
|
|
16
|
+
},
|
|
17
|
+
/* queryParams?: object, */
|
|
18
|
+
body?: any
|
|
19
|
+
}
|
|
20
|
+
): Promise<T> {
|
|
21
|
+
try {
|
|
22
|
+
let a: any;
|
|
23
|
+
let url: string = callParams.baseUrl + callParams.endPoint;
|
|
24
|
+
const privateAxiosInstance = axios.default.create();
|
|
25
|
+
|
|
26
|
+
if (callParams?.httpOptions?.headers) {
|
|
27
|
+
for (const headerKey in callParams.httpOptions.headers) {
|
|
28
|
+
if (callParams.httpOptions.headers[headerKey]) {
|
|
29
|
+
privateAxiosInstance.defaults.headers.common[headerKey] = callParams.httpOptions.headers[headerKey];
|
|
30
|
+
} else {
|
|
31
|
+
DBE_Shared.logError('DYNAMOBE ERROR: \n missing header:', headerKey, '\n\n', new Error());
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (inputParams?.pathParams) {
|
|
37
|
+
for (const pathParamKey in inputParams.pathParams) {
|
|
38
|
+
if (inputParams.pathParams[pathParamKey]) {
|
|
39
|
+
const paramType = typeof inputParams.pathParams[pathParamKey]
|
|
40
|
+
switch (paramType) {
|
|
41
|
+
case 'number':
|
|
42
|
+
url = url.replace(`{${pathParamKey}}`, inputParams.pathParams[pathParamKey].toString());
|
|
43
|
+
break;
|
|
44
|
+
case 'string':
|
|
45
|
+
url = url.replace(`{${pathParamKey}}`, inputParams.pathParams[pathParamKey]);
|
|
46
|
+
break;
|
|
47
|
+
default:
|
|
48
|
+
DBE_Shared.logError(`DYNAMOBE ERROR: \n wrong path param type: (${pathParamKey}): ${paramType} \n${inputParams.pathParams[pathParamKey]}\nMUST BE string or number\n\n`, new Error());
|
|
49
|
+
break;
|
|
50
|
+
}
|
|
51
|
+
} else {
|
|
52
|
+
DBE_Shared.logError('DYNAMOBE ERROR: \n missing pathParam:', pathParamKey, '\n\n', new Error());
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/* if (inputParams?.queryParams) {
|
|
58
|
+
const httpParams = new HttpParams();
|
|
59
|
+
for (const queryParamKey in inputParams.queryParams) {
|
|
60
|
+
if (inputParams.queryParams[queryParamKey]) {
|
|
61
|
+
switch (typeof inputParams.queryParams[queryParamKey]) {
|
|
62
|
+
case 'number':
|
|
63
|
+
httpParams.set(queryParamKey, inputParams.queryParams[queryParamKey].toString());
|
|
64
|
+
break;
|
|
65
|
+
case 'string':
|
|
66
|
+
httpParams.set(queryParamKey, inputParams.queryParams[queryParamKey]);
|
|
67
|
+
break;
|
|
68
|
+
default:
|
|
69
|
+
console.error('DYNAMOBE ERROR: \n wrong query param type', '\n\n', new Error());
|
|
70
|
+
break;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
callParams.httpOptions.params = httpParams;
|
|
75
|
+
} */
|
|
76
|
+
|
|
77
|
+
switch (callParams.type) {
|
|
78
|
+
case HttpCallType.get:
|
|
79
|
+
if (inputParams?.body) {
|
|
80
|
+
console.log('WARNING you cant send body in get calls');
|
|
81
|
+
}
|
|
82
|
+
await privateAxiosInstance.get<T>(url, callParams.httpOptions).then((res: axios.AxiosResponse ) => {
|
|
83
|
+
a = res.data;
|
|
84
|
+
console.log(`${callParams.name} was successful`, res.data);
|
|
85
|
+
});
|
|
86
|
+
break;
|
|
87
|
+
case HttpCallType.delete:
|
|
88
|
+
if (inputParams?.body) {
|
|
89
|
+
console.log('WARNING you cant send body in delete calls');
|
|
90
|
+
}
|
|
91
|
+
await privateAxiosInstance.delete<T>(url, callParams.httpOptions).then((res: axios.AxiosResponse ) => {
|
|
92
|
+
console.log(`${callParams.name} was successful`, res.data);
|
|
93
|
+
});
|
|
94
|
+
break;
|
|
95
|
+
case HttpCallType.post:
|
|
96
|
+
case HttpCallType.put:
|
|
97
|
+
case HttpCallType.patch:
|
|
98
|
+
if (!inputParams?.body) {
|
|
99
|
+
if (!inputParams) {
|
|
100
|
+
inputParams = {};
|
|
101
|
+
}
|
|
102
|
+
inputParams.body = {};
|
|
103
|
+
}
|
|
104
|
+
await privateAxiosInstance[callParams.type]<T>(url, inputParams.body, callParams.httpOptions).then((res: axios.AxiosResponse ) => {
|
|
105
|
+
a = res.data;
|
|
106
|
+
console.log(`${callParams.name} was successful`, res.data);
|
|
107
|
+
});
|
|
108
|
+
break;
|
|
109
|
+
default:
|
|
110
|
+
DBE_Shared.logError('DYNAMOBE ERROR: \n wrong api call type', '\n\n', new Error());
|
|
111
|
+
break;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
return a as T;
|
|
115
|
+
} catch (error) {
|
|
116
|
+
DBE_Shared.logError('API ERRROR');
|
|
117
|
+
if (callParams?.httpOptions?.responseType === HttpResponseType.text) {
|
|
118
|
+
error.error = JSON.parse(error.error);
|
|
119
|
+
}
|
|
120
|
+
if (error?.response?.data?.flag === 'DYNAMO-BE ERROR OBJECT') {
|
|
121
|
+
DBE_Shared.logError(`\n${callParams.name} was UNSUCCESSFUL\n\n`, new Error());
|
|
122
|
+
throw new DynamoBEError(error.response.data);
|
|
123
|
+
} else if (error.code === 'ENOTFOUND') {
|
|
124
|
+
DBE_Shared.logError(`\n${callParams.name} was UNSUCCESSFUL\n\n`, new Error());
|
|
125
|
+
throw new DynamoBEError({ message: 'DNS error; address cannot be resolved!', status: 404, error: error });
|
|
126
|
+
} else if (error.config && error.message) {
|
|
127
|
+
throw new DynamoBEError({ status: +error.message.substring(error.message.length - 4, 3), error: error });
|
|
128
|
+
} else {
|
|
129
|
+
DBE_Shared.logError(`\n${callParams.name} was UNSUCCESSFUL`, error, '\n\n', new Error());
|
|
130
|
+
throw error;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
|
|
2
|
+
import { DynamoBEAppParams } from '../_models/dynamobe-app-params';
|
|
3
|
+
import * as Mongoose from 'mongoose';
|
|
4
|
+
import { DBE_Shared } from './dynamobe-shared.service';
|
|
5
|
+
import { DynamoBESingletonService } from './dynamobe-singleton.service';
|
|
6
|
+
|
|
7
|
+
export abstract class DynamoBEApp extends DynamoBESingletonService {
|
|
8
|
+
|
|
9
|
+
protected params: DynamoBEAppParams;
|
|
10
|
+
|
|
11
|
+
protected mongoose = Mongoose;
|
|
12
|
+
|
|
13
|
+
constructor(){
|
|
14
|
+
super();
|
|
15
|
+
|
|
16
|
+
try {
|
|
17
|
+
this.setupAppParams();
|
|
18
|
+
console.log(`\n\n\nStarting ${this.params.name}... \nVersion: ${this.params.version}`);
|
|
19
|
+
if (this.params.title) console.log(this.params.title);
|
|
20
|
+
|
|
21
|
+
this.startDB();
|
|
22
|
+
|
|
23
|
+
this.setGlobalServiceCollection();
|
|
24
|
+
} catch (error) {
|
|
25
|
+
DBE_Shared.logError('\nApplication start failed.\n', error);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
private startDB(): void {
|
|
30
|
+
this.mongoose.connection
|
|
31
|
+
.on('error', (error) => {
|
|
32
|
+
DBE_Shared.logError('Unable to connect to MongoDB server, ERROR: ', error);
|
|
33
|
+
})
|
|
34
|
+
.once('open', () => {
|
|
35
|
+
DBE_Shared.logSuccess('\nConnected to MongoDB\n');
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
this.mongoose.connect(
|
|
39
|
+
this.params.dbUri,
|
|
40
|
+
{
|
|
41
|
+
useNewUrlParser: true,
|
|
42
|
+
useUnifiedTopology: true
|
|
43
|
+
}
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
abstract setupAppParams(): void;
|
|
48
|
+
abstract setGlobalServiceCollection(): void;
|
|
49
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
|
|
2
|
+
import { NextFunction, Request, Response } from 'express';
|
|
3
|
+
import { DynamoBEError } from '../_models/fullstack-models/control-models/dynamobe-error';
|
|
4
|
+
import { DynamoBESingletonService } from './dynamobe-singleton.service';
|
|
5
|
+
|
|
6
|
+
export abstract class DynamoBEAuthService extends DynamoBESingletonService {
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
*
|
|
10
|
+
* @param req
|
|
11
|
+
* @returns
|
|
12
|
+
*/
|
|
13
|
+
static getTokenFromRequest(req: Request): string {
|
|
14
|
+
const authHeader = req.headers['authorization'];
|
|
15
|
+
if (!authHeader) {
|
|
16
|
+
throw new DynamoBEError({ message: 'AuthHeader missing!' });
|
|
17
|
+
}
|
|
18
|
+
const token = authHeader.split(' ')[1];
|
|
19
|
+
if (!token) {
|
|
20
|
+
throw new DynamoBEError({ message: 'Token missing!' });
|
|
21
|
+
}
|
|
22
|
+
return token;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
abstract authenticateTokenSelf(req: Request, res: Response, next: NextFunction): Promise<void>;
|
|
26
|
+
abstract authenticateToken(req: Request, res: Response, next: NextFunction): Promise<void>;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Authenticate Token for Permission to Access UsageData
|
|
30
|
+
* @param req
|
|
31
|
+
* @param res
|
|
32
|
+
* @param next
|
|
33
|
+
*/
|
|
34
|
+
abstract authTokenPermAccUsageData(req: Request, res: Response, next: NextFunction): Promise<void>;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* The DynamoBE System is using this to get issuer, that will be set on DBServices
|
|
38
|
+
* @param req
|
|
39
|
+
*/
|
|
40
|
+
abstract getAccountIdFromRequest(req: Request): string;
|
|
41
|
+
abstract getUsernameFromRequest(req: Request): string;
|
|
42
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
import { DynamoBEEndpointParams } from '../_models/dynamobe-endpoint-control';
|
|
4
|
+
import { DynamoBESingletonService } from './dynamobe-singleton.service';
|
|
5
|
+
|
|
6
|
+
export abstract class DynamoBEController extends DynamoBESingletonService {
|
|
7
|
+
|
|
8
|
+
endpoints: DynamoBEEndpointParams[];
|
|
9
|
+
|
|
10
|
+
protected constructor(){
|
|
11
|
+
super();
|
|
12
|
+
|
|
13
|
+
this.setupEndpoints();
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* You must setup endpoints and required services in this function
|
|
18
|
+
*/
|
|
19
|
+
abstract setupEndpoints(): void
|
|
20
|
+
}
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
|
|
2
|
+
import { DynamoBEError } from '../_models/fullstack-models/control-models/dynamobe-error';
|
|
3
|
+
import { Metadata } from '../_models/fullstack-models/data-models/metadata';
|
|
4
|
+
import { DynamoBEDBService } from './dynamobe-db.service';
|
|
5
|
+
import { DynamoBEDataPropertyParams } from '../_models/fullstack-models/control-models/dynamobe-data-property-params';
|
|
6
|
+
import { DBE_Global_S } from './dynamobe-global.service';
|
|
7
|
+
|
|
8
|
+
export class DynamoBEDataService<T extends Metadata> {
|
|
9
|
+
dataDBService: DynamoBEDBService<T>;
|
|
10
|
+
data: T;
|
|
11
|
+
dataList: T[] = [];
|
|
12
|
+
issuer?: string;
|
|
13
|
+
|
|
14
|
+
depKey?: string;
|
|
15
|
+
depDBServiceKey?: string;
|
|
16
|
+
|
|
17
|
+
dataModelParams?: DynamoBEDataPropertyParams[] = [];
|
|
18
|
+
|
|
19
|
+
constructor(
|
|
20
|
+
dataDBService: DynamoBEDBService<T>,
|
|
21
|
+
data: T,
|
|
22
|
+
dataModelParams: DynamoBEDataPropertyParams[], // this will be used for dependency dataHook
|
|
23
|
+
issuer?: string,
|
|
24
|
+
) {
|
|
25
|
+
this.dataDBService = dataDBService;
|
|
26
|
+
this.data = data;
|
|
27
|
+
if (dataModelParams) {
|
|
28
|
+
this.dataModelParams = dataModelParams;
|
|
29
|
+
this.lookForDependencyDataSettings();
|
|
30
|
+
}
|
|
31
|
+
this.issuer = issuer;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* returns all data from database to service dataList
|
|
36
|
+
*/
|
|
37
|
+
async getAll(): Promise<void> {
|
|
38
|
+
try {
|
|
39
|
+
await this.dataDBService.getAll().then((dataList: T[]) => {
|
|
40
|
+
this.dataList = dataList;
|
|
41
|
+
});
|
|
42
|
+
} catch (error) {
|
|
43
|
+
throw new DynamoBEError({ status: 417, message: 'getAll was unsuccessful', error: error });
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* returns data from database by id
|
|
49
|
+
*/
|
|
50
|
+
async getDataById(dontSetToService?: boolean): Promise<T> {
|
|
51
|
+
try {
|
|
52
|
+
if (this.data._id) {
|
|
53
|
+
let dataExists: T;
|
|
54
|
+
await this.dataDBService.getDataById(this.data._id).then((data: T) => {
|
|
55
|
+
if (!dontSetToService) {
|
|
56
|
+
this.data = data;
|
|
57
|
+
}
|
|
58
|
+
dataExists = data;
|
|
59
|
+
});
|
|
60
|
+
return dataExists;
|
|
61
|
+
} else {
|
|
62
|
+
throw new DynamoBEError({ status: 417, message: 'ID is missing!' });
|
|
63
|
+
}
|
|
64
|
+
} catch (error) {
|
|
65
|
+
throw new DynamoBEError({ status: 417, message: 'getDataById was unsuccessful', error: error });
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* returns data from database by dependencyId to the service
|
|
71
|
+
* @param dependencyId
|
|
72
|
+
*/
|
|
73
|
+
async getDataByDependencyId(dependencyId?: string): Promise<void> {
|
|
74
|
+
try {
|
|
75
|
+
if (this.depKey) {
|
|
76
|
+
if (dependencyId || this.data[this.depKey]) {
|
|
77
|
+
await this.dataDBService.getDataByDependencyId(dependencyId ? dependencyId : this.data[this.depKey]).then((data: T) => {
|
|
78
|
+
this.data = data;
|
|
79
|
+
});
|
|
80
|
+
} else {
|
|
81
|
+
throw new DynamoBEError({ status: 417, message: `${this.depKey} is missing!` });
|
|
82
|
+
}
|
|
83
|
+
} else {
|
|
84
|
+
throw new DynamoBEError({ status: 417, message: 'dependencyDataIdKey is missing from service!' });
|
|
85
|
+
}
|
|
86
|
+
} catch (error) {
|
|
87
|
+
throw new DynamoBEError({ status: 417, message: 'getDataByDependencyId was unsuccessful', error: error });
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* returns dataList from database by dependencyId to the service
|
|
93
|
+
* @param dependencyId
|
|
94
|
+
*/
|
|
95
|
+
async getDataListByDependencyId(dependencyId?: string): Promise<void> {
|
|
96
|
+
try {
|
|
97
|
+
if (this.depKey) {
|
|
98
|
+
if (dependencyId || this.data[this.depKey]) {
|
|
99
|
+
await this.dataDBService.getDataListByDependencyId(dependencyId ? dependencyId : this.data[this.depKey]).then((data: T[]) => {
|
|
100
|
+
this.dataList = data;
|
|
101
|
+
});
|
|
102
|
+
} else {
|
|
103
|
+
throw new DynamoBEError({ status: 417, message: `${this.depKey} is missing!` });
|
|
104
|
+
}
|
|
105
|
+
} else {
|
|
106
|
+
throw new DynamoBEError({ status: 417, message: 'dependencyDataIdKey is missing from service!' });
|
|
107
|
+
}
|
|
108
|
+
} catch (error) {
|
|
109
|
+
throw new DynamoBEError({ status: 417, message: 'getDataListByDependencyId was unsuccessful', error: error });
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* modifies data if the data have ID and already exists in the DB,
|
|
115
|
+
* creates new if the ID is not present or cant find in DB,
|
|
116
|
+
* and if dependency data setted up, will check before creation
|
|
117
|
+
*/
|
|
118
|
+
async saveData(): Promise<void> {
|
|
119
|
+
try {
|
|
120
|
+
if (this.data._id) {
|
|
121
|
+
// check if already exists
|
|
122
|
+
let dataExists: T;
|
|
123
|
+
await this.getDataById(true).then((data: T) => {
|
|
124
|
+
dataExists = data;
|
|
125
|
+
}).catch(() => {});
|
|
126
|
+
if (!dataExists) {
|
|
127
|
+
// if data not exists check that dependency already exists for this id
|
|
128
|
+
// !!!!!!!!!!!!!!!!!! only if have depKey????
|
|
129
|
+
await this.getDependencyDataDBService().getDataById(this.data[this.depKey]);
|
|
130
|
+
// if data not exists create new data
|
|
131
|
+
await this.dataDBService.createData(this.data, this.issuer).then((data: T) => {
|
|
132
|
+
this.data = data; // !!!!!!!!! this is somehow returns the old data on api
|
|
133
|
+
// !!!!!!!!!!!!!!!!! if not using await for saveData??
|
|
134
|
+
});
|
|
135
|
+
} else {
|
|
136
|
+
// if data exists do modify
|
|
137
|
+
await this.dataDBService.modifyData(this.data, this.issuer).then((data: T) => {
|
|
138
|
+
this.data = data; // !!!!!!!!! this is somehow returns the old data on api
|
|
139
|
+
// !!!!!!!!!!!!!!!!! if not using await for saveData??
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
} else {
|
|
143
|
+
// if ID is not present, data not exists, create new data
|
|
144
|
+
await this.dataDBService.createData(this.data, this.issuer).then((data: T) => {
|
|
145
|
+
this.data = data;
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
} catch (error) {
|
|
149
|
+
throw new DynamoBEError({ status: 417, message: 'modifyData was unsuccessful', error: error });
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* permanently deletes data from database by data._id
|
|
155
|
+
*/
|
|
156
|
+
async deleteData(): Promise<void> {
|
|
157
|
+
try {
|
|
158
|
+
if (this.data._id) {
|
|
159
|
+
await this.dataDBService.deleteDataById(this.data._id);
|
|
160
|
+
} else {
|
|
161
|
+
throw new DynamoBEError({ status: 417, message: 'ID is missing!' });
|
|
162
|
+
}
|
|
163
|
+
} catch (error) {
|
|
164
|
+
throw new DynamoBEError({ status: 417, message: 'deleteData was unsuccessful', error: error });
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* validation of data, for modify and create, by the ModelParams
|
|
170
|
+
*/
|
|
171
|
+
async validateForSave(): Promise<void> {
|
|
172
|
+
try {
|
|
173
|
+
for (let i = 0; i < this.dataModelParams.length; i++) {
|
|
174
|
+
// basic required validations
|
|
175
|
+
if ((this.dataModelParams[i].required && (this.data[this.dataModelParams[i].key] === null || this.data[this.dataModelParams[i].key] === undefined)) ||
|
|
176
|
+
(this.dataModelParams[i].index && (this.data[this.dataModelParams[i].key] === null || this.data[this.dataModelParams[i].key] === undefined))) {
|
|
177
|
+
throw new DynamoBEError({ status: 422, message: `${this.dataModelParams[i].key} is missing! (${this.dataModelParams[i].required ? 'required' : 'index'})` });
|
|
178
|
+
}
|
|
179
|
+
// specific Date validation
|
|
180
|
+
if (this.dataModelParams[i].type === 'Date' && !(new Date(this.data[this.dataModelParams[i].key]) instanceof Date)) {
|
|
181
|
+
throw new DynamoBEError({ status: 422, message: `${this.dataModelParams[i].key} is not a date!` });
|
|
182
|
+
}
|
|
183
|
+
// call additional validators
|
|
184
|
+
if (this.dataModelParams[i].additionalValidators) {
|
|
185
|
+
for (let j = 0; j < this.dataModelParams[i].additionalValidators.length; j++) {
|
|
186
|
+
this.dataModelParams[i].additionalValidators[j](this.data[this.dataModelParams[i].key]);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
} catch (error) {
|
|
191
|
+
throw new DynamoBEError({ status: 422, message: 'validateForSave was unsuccessful', error: error });
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* setting up dependency dataHook by DynamoBEDataModelParams
|
|
197
|
+
*/
|
|
198
|
+
private lookForDependencyDataSettings(): void {
|
|
199
|
+
const dependencyParam: DynamoBEDataPropertyParams = this.dataModelParams.find((modelParams: DynamoBEDataPropertyParams) => modelParams.isDependencyHook);
|
|
200
|
+
if (dependencyParam) {
|
|
201
|
+
this.depKey = dependencyParam.key;
|
|
202
|
+
if (dependencyParam.dependencyDBServiceName) {
|
|
203
|
+
this.depDBServiceKey = dependencyParam.dependencyDBServiceName;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
getDependencyDataDBService(): DynamoBEDBService<any> {
|
|
209
|
+
if (this.depDBServiceKey) {
|
|
210
|
+
return DBE_Global_S.getDBServiceCollection()[this.depDBServiceKey];
|
|
211
|
+
} else {
|
|
212
|
+
throw new DynamoBEError({ status: 501, message: 'getDependencyDataDBService was unsuccessful, service key not setted up!' });
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
|
|
2
|
+
import { DynamoBEDBService } from './dynamobe-db.service';
|
|
3
|
+
import { DynamoBESingletonService } from './dynamobe-singleton.service';
|
|
4
|
+
|
|
5
|
+
export class DynamoBEDBServiceCollection extends DynamoBESingletonService {
|
|
6
|
+
|
|
7
|
+
[service: string]: DynamoBEDBService<any>;
|
|
8
|
+
|
|
9
|
+
}
|