@simitgroup/simpleapp-generator 2.0.2-a-alpha → 2.0.2-c-alpha
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/ReleaseNote.md +10 -0
- package/package.json +1 -1
- package/templates/basic/nest/controller.ts.eta +2 -2
- package/templates/nest/src/app.controller.ts.eta +10 -10
- package/templates/nest/src/app.module.ts._eta +1 -0
- package/templates/nest/src/simple-app/_core/features/auth/role-guard/roles.guard.ts.eta +5 -18
- package/templates/nest/src/simple-app/_core/features/document-no-format/document-no-format.service.ts.eta +16 -7
- package/templates/nest/src/simple-app/_core/features/log/log.service.ts.eta +10 -5
- package/templates/nest/src/simple-app/_core/features/mini-app/mini-app-scope/mini-app-scope.guard.ts.eta +4 -0
- package/templates/nest/src/simple-app/_core/features/profile/profile.service.ts.eta +15 -11
- package/templates/nest/src/simple-app/_core/features/queue/queue-base/queue-base.consumer.ts.eta +14 -13
- package/templates/nest/src/simple-app/_core/features/user-context/user.context.ts.eta +54 -3
- package/templates/nest/src/simple-app/_core/framework/base/simple-app.controller.ts.eta +21 -3
- package/templates/nest/src/simple-app/_core/framework/base/simple-app.service.ts.eta +401 -198
- package/templates/nest/src/simple-app/_core/framework/framework.module.ts.eta +3 -2
- package/templates/nest/src/simple-app/_core/framework/schemas/simple-app.schema.ts.eta +6 -0
- package/templates/nest/src/simple-app/_core/framework/simple-app-db-revert.service.ts.eta +101 -0
- package/templates/nest/src/simple-app/_core/framework/simple-app.interceptor.ts.eta +33 -12
|
@@ -3,10 +3,11 @@ import { SimpleAppInterceptor } from './simple-app.interceptor';
|
|
|
3
3
|
import { SimpleAppMiddleware } from './simple-app.middleware';
|
|
4
4
|
import { SimpleAppCoreFeaturesModule } from '../features/simple-app-core-features.module';
|
|
5
5
|
import {SimpleAppCoreResourcesModule} from '../resources/core-resources.module'
|
|
6
|
+
import { SimpleAppDbRevertService } from './simple-app-db-revert.service';
|
|
6
7
|
@Module({
|
|
7
8
|
imports: [SimpleAppCoreFeaturesModule,SimpleAppCoreResourcesModule],
|
|
8
9
|
controllers: [],
|
|
9
|
-
providers: [SimpleAppMiddleware, SimpleAppInterceptor],
|
|
10
|
-
exports: [SimpleAppCoreFeaturesModule,SimpleAppCoreResourcesModule,SimpleAppMiddleware, SimpleAppInterceptor],
|
|
10
|
+
providers: [SimpleAppMiddleware, SimpleAppInterceptor,SimpleAppDbRevertService],
|
|
11
|
+
exports: [SimpleAppCoreFeaturesModule,SimpleAppCoreResourcesModule,SimpleAppMiddleware, SimpleAppInterceptor,SimpleAppDbRevertService],
|
|
11
12
|
})
|
|
12
13
|
export class SimpleAppFrameworkModule {}
|
|
@@ -24,6 +24,10 @@ export class SearchBody {
|
|
|
24
24
|
|
|
25
25
|
sorts?: any[];
|
|
26
26
|
lookup?: Object;
|
|
27
|
+
pagination?: {
|
|
28
|
+
pageSize?: number;
|
|
29
|
+
pageNo?: number;
|
|
30
|
+
};
|
|
27
31
|
}
|
|
28
32
|
|
|
29
33
|
export enum IsolationType {
|
|
@@ -171,3 +175,5 @@ export class BranchPermission extends Permission {
|
|
|
171
175
|
@ApiProperty({ type: () => ForeignKey })
|
|
172
176
|
branch: ForeignKey;
|
|
173
177
|
}
|
|
178
|
+
|
|
179
|
+
export type StepData = {action:string,collection:string,id:string[],data:any[]}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file was automatically generated by simpleapp generator. Every
|
|
3
|
+
* MODIFICATION OVERRIDE BY GENERATEOR
|
|
4
|
+
* last change 2025-12-01
|
|
5
|
+
* Author: Ks Tan
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { BadRequestException, Injectable, Logger, NestMiddleware, ServiceUnavailableException } from '@nestjs/common';
|
|
9
|
+
import { InjectConnection, InjectModel } from '@nestjs/mongoose';
|
|
10
|
+
import { NextFunction, Request, Response } from 'express';
|
|
11
|
+
import { Model,Connection, ObjectId } from 'mongoose';
|
|
12
|
+
import { UserContext } from '../features/user-context/user.context';
|
|
13
|
+
import { StepData } from './schemas';
|
|
14
|
+
@Injectable()
|
|
15
|
+
export class SimpleAppDbRevertService{
|
|
16
|
+
|
|
17
|
+
private readonly logger = new Logger(this.constructor.name);
|
|
18
|
+
|
|
19
|
+
constructor(
|
|
20
|
+
@InjectConnection() private readonly connection: Connection,
|
|
21
|
+
) {}
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
async revertSteps(transSteps:StepData[]){
|
|
25
|
+
|
|
26
|
+
console.log("revert transSteps:",transSteps.length)
|
|
27
|
+
for(let i = transSteps.length-1; i>=0; i-- ){
|
|
28
|
+
const step = transSteps[i]
|
|
29
|
+
console.log("step",step)
|
|
30
|
+
switch(step.action){
|
|
31
|
+
case 'create':
|
|
32
|
+
await this.revertCreate(step)
|
|
33
|
+
break;
|
|
34
|
+
case 'update':
|
|
35
|
+
await this.revertUpdate(step)
|
|
36
|
+
break;
|
|
37
|
+
case 'patch':
|
|
38
|
+
await this.revertUpdate(step)
|
|
39
|
+
break;
|
|
40
|
+
case 'delete':
|
|
41
|
+
await this.revertDelete(step)
|
|
42
|
+
break;
|
|
43
|
+
case 'createMany':
|
|
44
|
+
await this.revertCreateMany(step)
|
|
45
|
+
break;
|
|
46
|
+
case 'updateMany':
|
|
47
|
+
await this.revertUpdateMany(step)
|
|
48
|
+
break;
|
|
49
|
+
case 'patchMany':
|
|
50
|
+
await this.revertUpdateMany(step)
|
|
51
|
+
break;
|
|
52
|
+
case 'deleteMany':
|
|
53
|
+
await this.revertDeleteMany(step)
|
|
54
|
+
break;
|
|
55
|
+
default:
|
|
56
|
+
break;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
async revertCreate(step:StepData){
|
|
62
|
+
const collection = this.connection.db.collection(step.collection)
|
|
63
|
+
await collection.deleteOne({_id:<any>step.id[0]})
|
|
64
|
+
console.log("revert create",step.collection,step.id)
|
|
65
|
+
}
|
|
66
|
+
async revertUpdate(step:StepData){
|
|
67
|
+
const collection = this.connection.db.collection(step.collection)
|
|
68
|
+
await collection.updateOne({_id:<any>step.id[0]},step.data[0])
|
|
69
|
+
console.log("revert update",step.collection,step.id)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
async revertDelete(step:StepData){
|
|
73
|
+
const collection = this.connection.db.collection(step.collection)
|
|
74
|
+
await collection.insertOne(step.data)
|
|
75
|
+
console.log("revert delete",step.collection,step.id)
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
async revertCreateMany(step:StepData){
|
|
80
|
+
const collection = this.connection.db.collection(step.collection)
|
|
81
|
+
await collection.deleteMany({_id: {$in: <any>step.id}})
|
|
82
|
+
console.log("revert createmany",step.collection,step.id)
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
async revertDeleteMany(step:StepData){
|
|
86
|
+
const collection = this.connection.db.collection(step.collection)
|
|
87
|
+
await collection.insertMany(step.data)
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
async revertUpdateMany(step:StepData){
|
|
91
|
+
const collection = this.connection.db.collection(step.collection)
|
|
92
|
+
for(let i=0; i < step.id.length;i++){
|
|
93
|
+
const id = step.id[i]
|
|
94
|
+
await collection.updateMany({_id: <any>id[i]},step.data[i])
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
|
|
@@ -6,19 +6,22 @@
|
|
|
6
6
|
*/
|
|
7
7
|
import { Injectable, NestInterceptor, ExecutionContext, CallHandler, BadGatewayException, HttpException } from '@nestjs/common';
|
|
8
8
|
import { Observable, throwError } from 'rxjs';
|
|
9
|
-
import { catchError, tap } from 'rxjs/operators';
|
|
9
|
+
import { catchError, tap, map } from 'rxjs/operators';
|
|
10
10
|
import { Model, Connection, ClientSession } from 'mongoose';
|
|
11
11
|
import { InjectConnection, InjectModel } from '@nestjs/mongoose';
|
|
12
12
|
import { UserContext } from '../features/user-context/user.context';
|
|
13
13
|
import { ApiEvent } from './schemas';
|
|
14
|
+
import { SimpleAppDbRevertService } from './simple-app-db-revert.service';
|
|
14
15
|
@Injectable()
|
|
15
16
|
export class SimpleAppInterceptor implements NestInterceptor {
|
|
16
17
|
constructor(
|
|
17
18
|
@InjectConnection() private readonly connection: Connection,
|
|
18
19
|
@InjectModel('ApiEvent') private apieventmodel: Model<ApiEvent>,
|
|
20
|
+
private simpleAppDbRevertService:SimpleAppDbRevertService
|
|
19
21
|
) {}
|
|
20
22
|
|
|
21
23
|
async intercept(context: ExecutionContext, next: CallHandler): Promise<Observable<any>> {
|
|
24
|
+
|
|
22
25
|
//not http request then exclude such as graphql
|
|
23
26
|
if (context.getType() != 'http') {
|
|
24
27
|
// obtain usersession here
|
|
@@ -28,10 +31,20 @@ export class SimpleAppInterceptor implements NestInterceptor {
|
|
|
28
31
|
}),
|
|
29
32
|
);
|
|
30
33
|
}
|
|
31
|
-
|
|
32
34
|
const req = context.switchToHttp().getRequest();
|
|
33
35
|
const resp = context.switchToHttp().getResponse();
|
|
34
36
|
//console.log("want to get user session:", Object.keys(req))
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
if (req.url == '/health') {
|
|
40
|
+
return next.handle().pipe(
|
|
41
|
+
tap(async () => {
|
|
42
|
+
//console.log("none http, do nothing at interceptor")
|
|
43
|
+
}),
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
|
|
35
48
|
const usersession: UserContext = req['sessionuser'];
|
|
36
49
|
//console.log("after read user session:",usersession)
|
|
37
50
|
const method = req['method'];
|
|
@@ -71,10 +84,18 @@ export class SimpleAppInterceptor implements NestInterceptor {
|
|
|
71
84
|
|
|
72
85
|
// req['eventObj'] = eventObj;
|
|
73
86
|
return next.handle().pipe(
|
|
87
|
+
map((data) => {
|
|
88
|
+
if (data && typeof data === 'object' && 'pagination' in data && 'items' in data) {
|
|
89
|
+
return data;
|
|
90
|
+
}
|
|
91
|
+
return data;
|
|
92
|
+
}),
|
|
74
93
|
catchError(async (err, caught) => {
|
|
75
94
|
// console.log('**************catchError at interceptor ',method,url)
|
|
76
|
-
if (
|
|
77
|
-
await
|
|
95
|
+
if (usersession.inTransaction()) {
|
|
96
|
+
// await usersession.rollBackTransaction();
|
|
97
|
+
await usersession.rollBackTransaction(this.simpleAppDbRevertService)
|
|
98
|
+
|
|
78
99
|
canCommit = false;
|
|
79
100
|
}
|
|
80
101
|
|
|
@@ -86,9 +107,9 @@ export class SimpleAppInterceptor implements NestInterceptor {
|
|
|
86
107
|
};
|
|
87
108
|
|
|
88
109
|
eventObj.statusCode = err.status;
|
|
89
|
-
eventObj.errMsg = responseBody.message
|
|
110
|
+
eventObj.errMsg = responseBody.message;
|
|
90
111
|
eventObj.data = req.body;
|
|
91
|
-
eventObj.status = 'NG'
|
|
112
|
+
eventObj.status = 'NG';
|
|
92
113
|
eventObj.errData = responseBody.error;
|
|
93
114
|
resp.status(err?.status ?? 500);
|
|
94
115
|
return responseBody;
|
|
@@ -105,16 +126,16 @@ export class SimpleAppInterceptor implements NestInterceptor {
|
|
|
105
126
|
await eventObj.save();
|
|
106
127
|
|
|
107
128
|
if (process.env.DRYRUN == 'true') {
|
|
108
|
-
console.warn('--------dryrun! roll back everything-----------');
|
|
109
|
-
if (
|
|
110
|
-
await
|
|
129
|
+
// console.warn('--------dryrun! roll back everything-----------');
|
|
130
|
+
if ( canCommit) {
|
|
131
|
+
await usersession.rollBackTransaction(this.simpleAppDbRevertService)
|
|
111
132
|
}
|
|
112
133
|
} else {
|
|
113
|
-
if (
|
|
114
|
-
await
|
|
134
|
+
if ( canCommit) {
|
|
135
|
+
await usersession.commitTransaction()
|
|
115
136
|
}
|
|
116
137
|
}
|
|
117
|
-
await
|
|
138
|
+
await usersession.endSession();
|
|
118
139
|
}),
|
|
119
140
|
);
|
|
120
141
|
}
|