@postxl/generators 2.3.0 → 3.1.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.
- package/dist/backend-authentication/authentication.generator.js +7 -0
- package/dist/backend-authentication/authentication.generator.js.map +1 -1
- package/dist/backend-authentication/generators/auth-guard.generator.js +11 -0
- package/dist/backend-authentication/generators/auth-guard.generator.js.map +1 -1
- package/dist/backend-core/template/apps/api/src/api.controller.ts +2 -0
- package/dist/backend-core/template/libs/e2e/src/e2e.controller.ts +3 -0
- package/dist/backend-core/template/libs/utils/src/public.decorator.ts +16 -0
- package/dist/backend-e2e/generators/e2e-controller.generator.js +3 -0
- package/dist/backend-e2e/generators/e2e-controller.generator.js.map +1 -1
- package/dist/backend-excel-io/excel-io.generator.js +12 -1
- package/dist/backend-excel-io/excel-io.generator.js.map +1 -1
- package/dist/backend-excel-io/generators/excel-io-service.generator.js +57 -7
- package/dist/backend-excel-io/generators/excel-io-service.generator.js.map +1 -1
- package/dist/backend-excel-io/template/excel-io.controller.ts +23 -3
- package/dist/backend-repositories/generators/expect-type.helper.d.ts +22 -0
- package/dist/backend-repositories/generators/expect-type.helper.js +26 -0
- package/dist/backend-repositories/generators/expect-type.helper.js.map +1 -0
- package/dist/backend-repositories/generators/model-repository.generator.js +137 -39
- package/dist/backend-repositories/generators/model-repository.generator.js.map +1 -1
- package/dist/backend-repositories/generators/repository-interface.generator.js +15 -1
- package/dist/backend-repositories/generators/repository-interface.generator.js.map +1 -1
- package/dist/backend-repositories/generators/row-conflict.generator.d.ts +10 -0
- package/dist/backend-repositories/generators/row-conflict.generator.js +93 -0
- package/dist/backend-repositories/generators/row-conflict.generator.js.map +1 -0
- package/dist/backend-repositories/repositories.generator.d.ts +9 -0
- package/dist/backend-repositories/repositories.generator.js +9 -0
- package/dist/backend-repositories/repositories.generator.js.map +1 -1
- package/dist/backend-rest-api/generators/model-controller.generator.js +10 -6
- package/dist/backend-rest-api/generators/model-controller.generator.js.map +1 -1
- package/dist/backend-rest-api/generators/rest-api-module.generator.js +4 -4
- package/dist/backend-rest-api/generators/rest-api-module.generator.js.map +1 -1
- package/dist/backend-rest-api/rest-api.generator.js +32 -3
- package/dist/backend-rest-api/rest-api.generator.js.map +1 -1
- package/dist/backend-rest-api/template/restApi/src/restApi.utils.ts +0 -9
- package/dist/backend-router-trpc/generators/excel-io-route.generator.js +3 -1
- package/dist/backend-router-trpc/generators/excel-io-route.generator.js.map +1 -1
- package/dist/backend-update/model-update-service.generator.js +21 -5
- package/dist/backend-update/model-update-service.generator.js.map +1 -1
- package/dist/frontend-core/generators/tsconfig.generator.js +4 -0
- package/dist/frontend-core/generators/tsconfig.generator.js.map +1 -1
- package/package.json +4 -4
|
@@ -60,7 +60,14 @@ exports.generator = {
|
|
|
60
60
|
name: Generator.toBackendModuleName('authentication'),
|
|
61
61
|
moduleClass: module,
|
|
62
62
|
apiModuleRegistration: {
|
|
63
|
+
import: Generator.ts(`
|
|
64
|
+
import { APP_GUARD } from '@nestjs/core'
|
|
65
|
+
import { AuthGuard } from '@authentication/auth.guard'
|
|
66
|
+
`),
|
|
63
67
|
code: Generator.ts(`AuthenticationModule.forRoot(config.auth)`),
|
|
68
|
+
// Deny by default: every controller - generated or hand-written - is authenticated
|
|
69
|
+
// unless it opts out with `@Public()` from `@utils/public.decorator`.
|
|
70
|
+
provider: [Generator.ts('{ provide: APP_GUARD, useClass: AuthGuard }')],
|
|
64
71
|
},
|
|
65
72
|
envConfig: {
|
|
66
73
|
imports: [Generator.ts(`import { parseStringList } from '@authentication/utils'`)],
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"authentication.generator.js","sourceRoot":"","sources":["../../src/backend-authentication/authentication.generator.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,yCAAmC;AAEnC,6DAA8C;AAE9C,wDAA2E;AAC3E,kDAA+E;AAC/E,kDAAkE;AAClE,kCAAmD;AAGnD,4EAA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,IAAA,mBAAO,EAAC,SAAS,EAAE,YAAY,CAAC,EAAE,CAAC,CAAA;QAEpE,GAAG,CAAC,KAAK,CAAC,oBAAoB,EAAE,SAAS,CAAC,gBAAgB,CAAC,gBAAgB,CAAC,CAAC,CAAA;QAE7E,OAAO,CAAC,GAAG,CAAC,aAAa,CAAC,EAAE,UAAU,EAAE,6BAA6B,EAAE,GAAG,EAAE,CAAC,CAAA;QAE7E,OAAO,OAAO,CAAA;IAChB,CAAC;CACF,CAAA"}
|
|
@@ -42,6 +42,8 @@ function generateAuthGuard(context) {
|
|
|
42
42
|
imports.add({ name: context.authentication.service.name, location: context.authentication.service._filePath });
|
|
43
43
|
return /* ts */ `
|
|
44
44
|
import { CanActivate, ExecutionContext, Injectable } from '@nestjs/common'
|
|
45
|
+
import { Reflector } from '@nestjs/core'
|
|
46
|
+
import { IS_PUBLIC_KEY } from '@utils/public.decorator'
|
|
45
47
|
|
|
46
48
|
import { FastifyRequest } from 'fastify'
|
|
47
49
|
|
|
@@ -56,12 +58,21 @@ export type FastifyRequestWithViewer = FastifyRequest & { viewer: Viewer }
|
|
|
56
58
|
@Injectable()
|
|
57
59
|
export class ${context.authentication.guard.name} implements CanActivate {
|
|
58
60
|
constructor(
|
|
61
|
+
private readonly reflector: Reflector,
|
|
59
62
|
private readonly authService: ${context.authentication.service.name},
|
|
60
63
|
private readonly viewService: ${context.view.service.name},
|
|
61
64
|
private readonly dispatcherService: ${context.actions.dispatcher.name},
|
|
62
65
|
) {}
|
|
63
66
|
|
|
64
67
|
public async canActivate(context: ExecutionContext): Promise<boolean> {
|
|
68
|
+
const isPublic = this.reflector.getAllAndOverride<boolean>(IS_PUBLIC_KEY, [
|
|
69
|
+
context.getHandler(),
|
|
70
|
+
context.getClass(),
|
|
71
|
+
])
|
|
72
|
+
if (isPublic) {
|
|
73
|
+
return true
|
|
74
|
+
}
|
|
75
|
+
|
|
65
76
|
const http = context.switchToHttp()
|
|
66
77
|
const req = http.getRequest<FastifyRequestWithViewer>()
|
|
67
78
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"auth-guard.generator.js","sourceRoot":"","sources":["../../../src/backend-authentication/generators/auth-guard.generator.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAIA,
|
|
1
|
+
{"version":3,"file":"auth-guard.generator.js","sourceRoot":"","sources":["../../../src/backend-authentication/generators/auth-guard.generator.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAIA,8CA2FC;AA/FD,6DAA8C;AAI9C,SAAgB,iBAAiB,CAAC,OAAsB;IACtD,MAAM,OAAO,GAAG,SAAS,CAAC,eAAe,CAAC,IAAI,CAAC,OAAO,CAAC,cAAc,CAAC,KAAK,CAAC,SAAS,CAAC,CAAA;IAEtF,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,CAAA;IACjC,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,OAAO,CAAC,UAAU,CAAC,CAAA;IACvC,OAAO,CAAC,GAAG,CAAC,EAAE,IAAI,EAAE,OAAO,CAAC,cAAc,CAAC,OAAO,CAAC,IAAI,EAAE,QAAQ,EAAE,OAAO,CAAC,cAAc,CAAC,OAAO,CAAC,SAAS,EAAE,CAAC,CAAA;IAC9G,OAAO,QAAQ,CAAC;;;;;;;EAOhB,OAAO,CAAC,QAAQ,EAAE;;;;;;;;;eASL,OAAO,CAAC,cAAc,CAAC,KAAK,CAAC,IAAI;;;oCAGZ,OAAO,CAAC,cAAc,CAAC,OAAO,CAAC,IAAI;oCACnC,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI;0CACnB,OAAO,CAAC,OAAO,CAAC,UAAU,CAAC,IAAI;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EA+DvE,CAAA;AACF,CAAC"}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Controller, Get, Logger } from '@nestjs/common'
|
|
2
|
+
import { Public } from '@utils/public.decorator'
|
|
2
3
|
|
|
3
4
|
@Controller('/')
|
|
4
5
|
export class ApiController {
|
|
@@ -7,6 +8,7 @@ export class ApiController {
|
|
|
7
8
|
// eslint-disable-next-line @typescript-eslint/no-useless-constructor, @typescript-eslint/no-empty-function
|
|
8
9
|
constructor() {}
|
|
9
10
|
|
|
11
|
+
@Public()
|
|
10
12
|
@Get('health')
|
|
11
13
|
health(): string {
|
|
12
14
|
return 'OK'
|
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
import { Controller, Get, Logger, Put, Res } from '@nestjs/common'
|
|
2
|
+
import { Public } from '@utils/public.decorator'
|
|
2
3
|
|
|
3
4
|
import { FastifyReply } from 'fastify'
|
|
4
5
|
|
|
5
6
|
import { TestDataService } from './test-data.service'
|
|
6
7
|
|
|
8
|
+
// This controller is only registered when `config.test.isTest` is true.
|
|
9
|
+
@Public()
|
|
7
10
|
@Controller('test')
|
|
8
11
|
export class E2EController {
|
|
9
12
|
private readonly logger = new Logger(E2EController.name)
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { SetMetadata } from '@nestjs/common'
|
|
2
|
+
|
|
3
|
+
export const IS_PUBLIC_KEY = 'pxl:isPublic'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Marks a route handler (or a whole controller) as reachable without authentication.
|
|
7
|
+
*
|
|
8
|
+
* The generated backend registers `AuthGuard` as an `APP_GUARD`, so every controller -
|
|
9
|
+
* generated or hand-written - denies anonymous requests by default. Opting a route out
|
|
10
|
+
* of that is an explicit, greppable decision:
|
|
11
|
+
*
|
|
12
|
+
* ```bash
|
|
13
|
+
* grep -rn '@Public()' backend/
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
export const Public = () => SetMetadata(IS_PUBLIC_KEY, true)
|
|
@@ -43,10 +43,13 @@ function generateBackendE2eController(context) {
|
|
|
43
43
|
.add(service);
|
|
44
44
|
return `
|
|
45
45
|
import { Body, Controller, Get, Logger, Put, Res } from '@nestjs/common'
|
|
46
|
+
import { Public } from '@utils/public.decorator'
|
|
46
47
|
import { FastifyReply } from 'fastify'
|
|
47
48
|
|
|
48
49
|
${imports.generate()}
|
|
49
50
|
|
|
51
|
+
// This controller is only registered when \`config.test.isTest\` is true.
|
|
52
|
+
@Public()
|
|
50
53
|
@Controller('test')
|
|
51
54
|
export class ${controller.name} {
|
|
52
55
|
private logger = new Logger(${controller.name}.name)
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"e2e-controller.generator.js","sourceRoot":"","sources":["../../../src/backend-e2e/generators/e2e-controller.generator.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAIA,
|
|
1
|
+
{"version":3,"file":"e2e-controller.generator.js","sourceRoot":"","sources":["../../../src/backend-e2e/generators/e2e-controller.generator.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAIA,oEA0CC;AA9CD,6DAA8C;AAI9C,SAAgB,4BAA4B,CAAC,OAAsB;IACjE,MAAM,EAAE,UAAU,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC,UAAU,CAAA;IAClD,MAAM,OAAO,GAAG,SAAS,CAAC,eAAe;QACvC,EAAE;SACD,IAAI,CAAC,UAAU,CAAC,QAAQ,CAAC;SACzB,GAAG,CAAC,OAAO,CAAC,CAAA;IAEf,OAAO;;;;;EAKP,OAAO,CAAC,QAAQ,EAAE;;;;;eAKL,UAAU,CAAC,IAAI;gCACE,UAAU,CAAC,IAAI;kCACb,OAAO,CAAC,IAAI;;;;;;;;;;;;;;;;;;;;;;EAsB5C,CAAA;AACF,CAAC"}
|
|
@@ -64,7 +64,18 @@ exports.generator = {
|
|
|
64
64
|
name: Generator.toBackendModuleName('excel-io'),
|
|
65
65
|
moduleClass: module,
|
|
66
66
|
apiModuleRegistration: {
|
|
67
|
-
code: (0, generator_1.ts)('ExcelIoModule'),
|
|
67
|
+
code: (0, generator_1.ts)('...(config.excelIo.enabled ? [ExcelIoModule] : [])'),
|
|
68
|
+
},
|
|
69
|
+
envConfig: {
|
|
70
|
+
decoder: (0, generator_1.ts)(`
|
|
71
|
+
EXCEL_IO_ENABLED: zEnvBoolean.optional().default(false),`),
|
|
72
|
+
transformer: (0, generator_1.ts)(`
|
|
73
|
+
excelIo: {
|
|
74
|
+
enabled: val.EXCEL_IO_ENABLED,
|
|
75
|
+
}`),
|
|
76
|
+
dotEnvExample: `
|
|
77
|
+
EXCEL_IO_ENABLED=false
|
|
78
|
+
`,
|
|
68
79
|
},
|
|
69
80
|
};
|
|
70
81
|
context.backend.modules.push(excelIoModule);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"excel-io.generator.js","sourceRoot":"","sources":["../../src/backend-excel-io/excel-io.generator.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,gDAAiC;AAEjC,6DAA8C;AAC9C,iDAAsC;AAEtC,wDAA2E;AAC3E,kDAA+E;AAC/E,sDAAwE;AACxE,kDAAkE;AAClE,0CAA+D;AAC/D,oCAAsD;AAEtD,wFAAgF;AAYnE,QAAA,WAAW,GAAG,SAAS,CAAC,sBAAsB,CAAC,kBAAkB,CAAC,CAAA;AAElE,QAAA,SAAS,GAAiC;IACrD,EAAE,EAAE,mBAAW;IACf,QAAQ,EAAE;QACR,iCAAkB;QAClB,2CAAyB;QACzB,qCAAsB;QACtB,yCAAwB;QACxB,8BAAmB;QACnB,wBAAgB;KACjB;IAED,QAAQ,EAAE,CAAsC,OAAgB,EAAiB,EAAE;QACjF,MAAM,MAAM,GAA8B;YACxC,IAAI,EAAE,SAAS,CAAC,WAAW,CAAC,eAAe,CAAC;YAC5C,QAAQ,EAAE,SAAS,CAAC,uBAAuB,CAAC,2BAA2B,CAAC;SACzE,CAAA;QAED,MAAM,aAAa,GAAiB;YAClC,IAAI,EAAE,S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OAAO,CAAC,EAAE,IAAA,mDAAsB,EAAC,OAAO,CAAC,CAAC,CAAA;QAE7F,MAAM,CAAC,KAAK,CACV,UAAU,EACV;;;CAGL,CACI,CAAA;QAED,MAAM,GAAG,GAAG,IAAI,SAAS,CAAC,iBAAiB,EAAE,CAAA;QAC7C,GAAG,CAAC,aAAa,CAAC,EAAE,UAAU,EAAE,KAAK,EAAE,GAAG,EAAE,MAAM,EAAE,CAAC,CAAA;QAErD,GAAG,CAAC,KAAK,CAAC,oBAAoB,EAAE,SAAS,CAAC,gBAAgB,CAAC,UAAU,CAAC,CAAC,CAAA;QAEvE,OAAO,CAAC,GAAG,CAAC,aAAa,CAAC,EAAE,UAAU,EAAE,uBAAuB,EAAE,GAAG,EAAE,CAAC,CAAA;QAEvE,OAAO,OAAO,CAAA;IAChB,CAAC;CACF,CAAA"}
|
|
@@ -30,13 +30,14 @@ function generateExcelIoService(context) {
|
|
|
30
30
|
.map((model) => ` ${model.types.filter.decoder.name},\n ${model.types.sort.decoder.name},`)
|
|
31
31
|
.join('\n');
|
|
32
32
|
return /*ts*/ `
|
|
33
|
+
import { ${context.actions.authorization.policy.name} } from '${context.actions.authorization.policy.location}'
|
|
33
34
|
import { DispatcherService } from '@actions/dispatcher.service'
|
|
34
35
|
import { flattenExcelDataModel, type DataModel, type RecordParseError } from '@decoders/data-model.decoder'
|
|
35
36
|
import type { Action_Import, ImportExecutionSummary } from '@import/import.service'
|
|
36
37
|
import { ImportService } from '@import/import.service'
|
|
37
38
|
import type { Delta } from '@import/detect-delta/models.detect-delta'
|
|
38
39
|
import { extractErrors } from '@import/detect-delta/models.detect-delta'
|
|
39
|
-
import { forwardRef, Inject, Injectable, Logger } from '@nestjs/common'
|
|
40
|
+
import { ForbiddenException, forwardRef, Inject, Injectable, Logger } from '@nestjs/common'
|
|
40
41
|
import type { AuthorizationContext } from '@types'
|
|
41
42
|
import {
|
|
42
43
|
${filterSortImports}
|
|
@@ -70,6 +71,8 @@ type StoredPreview = {
|
|
|
70
71
|
includeDetailedUnchangedRecords: boolean
|
|
71
72
|
filename: string
|
|
72
73
|
createdAt: number
|
|
74
|
+
/** The user that created this preview. Only they may read or execute it. */
|
|
75
|
+
ownerId: string
|
|
73
76
|
}
|
|
74
77
|
|
|
75
78
|
export type ImportPreviewModelSummary = {
|
|
@@ -119,6 +122,17 @@ export const zImportPreviewSessionRequestDecoder = z.object({
|
|
|
119
122
|
|
|
120
123
|
export type ImportPreviewSessionRequest = z.infer<typeof zImportPreviewSessionRequestDecoder>
|
|
121
124
|
|
|
125
|
+
/**
|
|
126
|
+
* The action scope that governs access to the whole excel-io surface. Configure it under
|
|
127
|
+
* \`auth.scopes.import\` in the project schema; with \`defaultDeny\` (the default) an
|
|
128
|
+
* unconfigured project denies every caller.
|
|
129
|
+
*/
|
|
130
|
+
const EXCEL_IO_SCOPE = 'import' as const
|
|
131
|
+
const EXCEL_IO_ACTION_TYPE = 'excel' as const
|
|
132
|
+
|
|
133
|
+
/** Deliberately identical for an unknown, expired and foreign-owned session id. */
|
|
134
|
+
const INVALID_PREVIEW_SESSION_MESSAGE = 'Invalid or expired preview session id'
|
|
135
|
+
|
|
122
136
|
export const zImportExecuteRequestDecoder = z.object({
|
|
123
137
|
previewSessionId: z.string().min(1, 'Preview session id is required'),
|
|
124
138
|
ignoreErrors: z.boolean().default(false),
|
|
@@ -136,15 +150,32 @@ export class ExcelIoService {
|
|
|
136
150
|
constructor(
|
|
137
151
|
private readonly viewService: ViewService,
|
|
138
152
|
private readonly xlPortService: XlPortService,
|
|
153
|
+
private readonly authorizationPolicy: ${context.actions.authorization.policy.name},
|
|
139
154
|
@Inject(forwardRef(() => DispatcherService)) private readonly dispatcherService: DispatcherService,
|
|
140
155
|
@Inject(forwardRef(() => ImportService)) private readonly importService: ImportService,
|
|
141
156
|
) {}
|
|
142
157
|
|
|
158
|
+
/**
|
|
159
|
+
* Bulk export/import is an instance-wide surface, so authentication alone is not enough:
|
|
160
|
+
* every entry point requires the roles granted by the \`import\` action scope.
|
|
161
|
+
*/
|
|
162
|
+
private assertAuthorized(authorization: AuthorizationContext): void {
|
|
163
|
+
const allowed = this.authorizationPolicy.canRunAction({
|
|
164
|
+
authorization,
|
|
165
|
+
scope: EXCEL_IO_SCOPE,
|
|
166
|
+
actionType: EXCEL_IO_ACTION_TYPE,
|
|
167
|
+
})
|
|
168
|
+
if (!allowed) {
|
|
169
|
+
throw new ForbiddenException('Not allowed to use the Excel import/export surface')
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
143
173
|
public parseModelName(model: string): ExportModelName {
|
|
144
174
|
return modelNameDecoder.parse(model)
|
|
145
175
|
}
|
|
146
176
|
|
|
147
177
|
public async exportAllModelsToExcel(authorization: AuthorizationContext) {
|
|
178
|
+
this.assertAuthorized(authorization)
|
|
148
179
|
this.logger.log('Exporting all models to Excel')
|
|
149
180
|
|
|
150
181
|
const dataModel: DataModel = {
|
|
@@ -165,6 +196,7 @@ ${allModelsDataModel}
|
|
|
165
196
|
rawSort?: unknown
|
|
166
197
|
authorization: AuthorizationContext
|
|
167
198
|
}) {
|
|
199
|
+
this.assertAuthorized(authorization)
|
|
168
200
|
this.logger.log(\`Exporting model ${'${model}'} to Excel\`)
|
|
169
201
|
|
|
170
202
|
switch (model) {${modelCases}
|
|
@@ -182,6 +214,7 @@ ${allModelsDataModel}
|
|
|
182
214
|
filename: string
|
|
183
215
|
authorization: AuthorizationContext
|
|
184
216
|
}) {
|
|
217
|
+
this.assertAuthorized(authorization)
|
|
185
218
|
this.logger.log('Importing uploaded Excel file through import action')
|
|
186
219
|
|
|
187
220
|
const action: Action_Import = {
|
|
@@ -203,12 +236,17 @@ ${allModelsDataModel}
|
|
|
203
236
|
public async previewImportExcel({
|
|
204
237
|
data,
|
|
205
238
|
filename,
|
|
239
|
+
authorization,
|
|
206
240
|
includeDetailedUnchangedRecords = false,
|
|
207
241
|
}: {
|
|
208
242
|
data: Buffer
|
|
209
243
|
filename: string
|
|
244
|
+
authorization: AuthorizationContext
|
|
210
245
|
includeDetailedUnchangedRecords?: boolean
|
|
211
246
|
}): Promise<ImportPreviewSessionResponse> {
|
|
247
|
+
// The delta is computed against the raw data layer, so this must stay behind the
|
|
248
|
+
// scope check above - it is an instance-wide read, not a viewer-scoped one.
|
|
249
|
+
this.assertAuthorized(authorization)
|
|
212
250
|
this.logger.log('Previewing Excel import (read-only)')
|
|
213
251
|
|
|
214
252
|
try {
|
|
@@ -222,6 +260,7 @@ ${allModelsDataModel}
|
|
|
222
260
|
includeDetailedUnchangedRecords,
|
|
223
261
|
filename,
|
|
224
262
|
createdAt: Date.now(),
|
|
263
|
+
ownerId: String(authorization.user.id),
|
|
225
264
|
})
|
|
226
265
|
|
|
227
266
|
return { previewSessionId }
|
|
@@ -231,9 +270,13 @@ ${allModelsDataModel}
|
|
|
231
270
|
}
|
|
232
271
|
}
|
|
233
272
|
|
|
234
|
-
public getImportPreviewBySessionId({
|
|
273
|
+
public getImportPreviewBySessionId({
|
|
274
|
+
previewSessionId,
|
|
275
|
+
authorization,
|
|
276
|
+
}: ImportPreviewSessionRequest & { authorization: AuthorizationContext }): ImportPreviewResponse {
|
|
277
|
+
this.assertAuthorized(authorization)
|
|
235
278
|
this.logger.log(\`Retrieving import preview session: \${previewSessionId}\`)
|
|
236
|
-
const stored = this.getStoredPreview(previewSessionId)
|
|
279
|
+
const stored = this.getStoredPreview(previewSessionId, authorization)
|
|
237
280
|
return buildPreviewResponse(
|
|
238
281
|
previewSessionId,
|
|
239
282
|
stored.filename,
|
|
@@ -250,8 +293,9 @@ ${allModelsDataModel}
|
|
|
250
293
|
selectedDeltaIndicesByModel,
|
|
251
294
|
authorization,
|
|
252
295
|
}: ImportExecuteRequest & { authorization: AuthorizationContext }): Promise<ImportExecutionSummary> {
|
|
296
|
+
this.assertAuthorized(authorization)
|
|
253
297
|
this.logger.log(\`Executing import from preview session: \${previewSessionId}\`)
|
|
254
|
-
const stored = this.getStoredPreview(previewSessionId)
|
|
298
|
+
const stored = this.getStoredPreview(previewSessionId, authorization)
|
|
255
299
|
const selectedDelta = filterDeltaBySelectedIndices(stored.delta, selectedDeltaIndicesByModel)
|
|
256
300
|
|
|
257
301
|
if (!deltaHasExecutableChanges(selectedDelta)) {
|
|
@@ -289,17 +333,23 @@ ${allModelsDataModel}
|
|
|
289
333
|
return result as ImportExecutionSummary
|
|
290
334
|
}
|
|
291
335
|
|
|
292
|
-
private getStoredPreview(previewSessionId: string): StoredPreview {
|
|
336
|
+
private getStoredPreview(previewSessionId: string, authorization: AuthorizationContext): StoredPreview {
|
|
293
337
|
this.cleanupExpiredPreviews()
|
|
294
338
|
|
|
295
339
|
const stored = this.previewStore.get(previewSessionId)
|
|
296
340
|
if (!stored) {
|
|
297
|
-
throw new Error(
|
|
341
|
+
throw new Error(INVALID_PREVIEW_SESSION_MESSAGE)
|
|
298
342
|
}
|
|
299
343
|
|
|
300
344
|
if (Date.now() - stored.createdAt > PREVIEW_TTL_MS) {
|
|
301
345
|
this.previewStore.delete(previewSessionId)
|
|
302
|
-
throw new Error(
|
|
346
|
+
throw new Error(INVALID_PREVIEW_SESSION_MESSAGE)
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
// Same error as an unknown id, so the id space is not an existence oracle over
|
|
350
|
+
// other users' in-flight imports.
|
|
351
|
+
if (stored.ownerId !== String(authorization.user.id)) {
|
|
352
|
+
throw new Error(INVALID_PREVIEW_SESSION_MESSAGE)
|
|
303
353
|
}
|
|
304
354
|
|
|
305
355
|
return stored
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"excel-io-service.generator.js","sourceRoot":"","sources":["../../../src/backend-excel-io/generators/excel-io-service.generator.ts"],"names":[],"mappings":";;AAUA,
|
|
1
|
+
{"version":3,"file":"excel-io-service.generator.js","sourceRoot":"","sources":["../../../src/backend-excel-io/generators/excel-io-service.generator.ts"],"names":[],"mappings":";;AAUA,wDAgvBC;AAhvBD,SAAgB,sBAAsB,CAAC,OAA4B;IACjE,MAAM,M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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA8hB/B,CAAA;AACD,CAAC"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { AuthGuard, type Viewer } from '@authentication/auth.guard'
|
|
2
2
|
import { ViewerParam } from '@authentication/viewer.decorator'
|
|
3
|
-
import { Controller, Get, Logger, Param, Post, Query, Res, UseGuards } from '@nestjs/common'
|
|
3
|
+
import { Controller, Get, HttpException, Logger, Param, Post, Query, Res, UseGuards } from '@nestjs/common'
|
|
4
4
|
import { UploadGuard } from '@upload/upload.guard'
|
|
5
5
|
import { UploadedFileData } from '@upload/uploaded-file.decorator'
|
|
6
6
|
import type { UploadedFileDataPayload } from '@upload/upload.types'
|
|
@@ -13,7 +13,14 @@ const MAX_IMPORT_FILE_SIZE_BYTES = 20 * 1024 * 1024 // 20 MB
|
|
|
13
13
|
|
|
14
14
|
/**
|
|
15
15
|
* Security note:
|
|
16
|
-
* This
|
|
16
|
+
* This is an instance-wide bulk export/import surface: `export/all` walks every model and an
|
|
17
|
+
* import preview reads every table. It is therefore:
|
|
18
|
+
*
|
|
19
|
+
* - registered only when `EXCEL_IO_ENABLED=true` (see `api.module.ts`),
|
|
20
|
+
* - authenticated by the global `AuthGuard` (`APP_GUARD`), and
|
|
21
|
+
* - authorized in `ExcelIoService` against the `import` action scope, so a merely
|
|
22
|
+
* logged-in user is not enough - the caller needs the roles that scope grants.
|
|
23
|
+
*
|
|
17
24
|
* In local dev with AUTH=false, mock auth still provides a viewer context.
|
|
18
25
|
*/
|
|
19
26
|
@UseGuards(AuthGuard)
|
|
@@ -34,6 +41,9 @@ export class ExcelIoController {
|
|
|
34
41
|
res.header('Access-Control-Expose-Headers', 'Content-Disposition')
|
|
35
42
|
res.send(file)
|
|
36
43
|
} catch (error) {
|
|
44
|
+
if (error instanceof HttpException) {
|
|
45
|
+
throw error
|
|
46
|
+
}
|
|
37
47
|
this.logger.error('Error exporting all models to Excel', error)
|
|
38
48
|
res.status(500).send({ error: error instanceof Error ? error.message : String(error) })
|
|
39
49
|
}
|
|
@@ -65,6 +75,9 @@ export class ExcelIoController {
|
|
|
65
75
|
res.header('Access-Control-Expose-Headers', 'Content-Disposition')
|
|
66
76
|
res.send(file)
|
|
67
77
|
} catch (error) {
|
|
78
|
+
if (error instanceof HttpException) {
|
|
79
|
+
throw error
|
|
80
|
+
}
|
|
68
81
|
this.logger.error(`Error exporting model ${model} to Excel`, error)
|
|
69
82
|
res.status(500).send({ error: error instanceof Error ? error.message : String(error) })
|
|
70
83
|
}
|
|
@@ -91,6 +104,9 @@ export class ExcelIoController {
|
|
|
91
104
|
|
|
92
105
|
res.status(200).send({ success: true, result })
|
|
93
106
|
} catch (error) {
|
|
107
|
+
if (error instanceof HttpException) {
|
|
108
|
+
throw error
|
|
109
|
+
}
|
|
94
110
|
this.logger.error('Error importing Excel file', error)
|
|
95
111
|
res.status(500).send({ success: false, error: error instanceof Error ? error.message : String(error) })
|
|
96
112
|
}
|
|
@@ -104,7 +120,7 @@ export class ExcelIoController {
|
|
|
104
120
|
}),
|
|
105
121
|
)
|
|
106
122
|
async previewImportExcel(
|
|
107
|
-
@ViewerParam()
|
|
123
|
+
@ViewerParam() viewer: Viewer,
|
|
108
124
|
@UploadedFileData() uploadData: UploadedFileDataPayload,
|
|
109
125
|
@Res({ passthrough: true }) res: FastifyReply,
|
|
110
126
|
): Promise<void> {
|
|
@@ -112,6 +128,7 @@ export class ExcelIoController {
|
|
|
112
128
|
const result = await this.excelIoService.previewImportExcel({
|
|
113
129
|
data: uploadData.buffer,
|
|
114
130
|
filename: uploadData.filename,
|
|
131
|
+
authorization: viewer,
|
|
115
132
|
includeDetailedUnchangedRecords: parseMultipartBooleanField(
|
|
116
133
|
uploadData.fields,
|
|
117
134
|
'includeDetailedUnchangedRecords',
|
|
@@ -121,6 +138,9 @@ export class ExcelIoController {
|
|
|
121
138
|
|
|
122
139
|
res.status(200).send({ success: true, result })
|
|
123
140
|
} catch (error) {
|
|
141
|
+
if (error instanceof HttpException) {
|
|
142
|
+
throw error
|
|
143
|
+
}
|
|
124
144
|
this.logger.error('Error previewing Excel import', error)
|
|
125
145
|
res.status(500).send({ success: false, error: error instanceof Error ? error.message : String(error) })
|
|
126
146
|
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { Field } from '@postxl/schema';
|
|
2
|
+
type ModelForExpectType = {
|
|
3
|
+
fields: Map<string, Field>;
|
|
4
|
+
types: {
|
|
5
|
+
name: string;
|
|
6
|
+
};
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* The type of a compare-and-set precondition for a model.
|
|
10
|
+
*
|
|
11
|
+
* JSON fields are deliberately excluded. The database path compares them with
|
|
12
|
+
* Postgres `jsonb` equality (key-order insensitive, duplicate keys collapsed);
|
|
13
|
+
* the in-memory path can only compare values in JS. The two execution modes
|
|
14
|
+
* could not be made to agree, which would break exactly the parity guarantee
|
|
15
|
+
* `expect` exists to provide - so a JSON field cannot be guarded on at all.
|
|
16
|
+
* Guard on a scalar, or on `updatedAt` as a whole-row version, instead.
|
|
17
|
+
*
|
|
18
|
+
* Note this is also what keeps a `Json` field typed `unknown` in the model from
|
|
19
|
+
* silently accepting anything a caller passes.
|
|
20
|
+
*/
|
|
21
|
+
export declare function generateExpectType(model: ModelForExpectType): string;
|
|
22
|
+
export {};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.generateExpectType = generateExpectType;
|
|
4
|
+
/**
|
|
5
|
+
* The type of a compare-and-set precondition for a model.
|
|
6
|
+
*
|
|
7
|
+
* JSON fields are deliberately excluded. The database path compares them with
|
|
8
|
+
* Postgres `jsonb` equality (key-order insensitive, duplicate keys collapsed);
|
|
9
|
+
* the in-memory path can only compare values in JS. The two execution modes
|
|
10
|
+
* could not be made to agree, which would break exactly the parity guarantee
|
|
11
|
+
* `expect` exists to provide - so a JSON field cannot be guarded on at all.
|
|
12
|
+
* Guard on a scalar, or on `updatedAt` as a whole-row version, instead.
|
|
13
|
+
*
|
|
14
|
+
* Note this is also what keeps a `Json` field typed `unknown` in the model from
|
|
15
|
+
* silently accepting anything a caller passes.
|
|
16
|
+
*/
|
|
17
|
+
function generateExpectType(model) {
|
|
18
|
+
const jsonFieldNames = Array.from(model.fields.values())
|
|
19
|
+
.filter((field) => field.kind === 'json')
|
|
20
|
+
.map((field) => `'${field.name}'`);
|
|
21
|
+
if (jsonFieldNames.length === 0) {
|
|
22
|
+
return `Partial<${model.types.name}>`;
|
|
23
|
+
}
|
|
24
|
+
return `Partial<Omit<${model.types.name}, ${jsonFieldNames.join(' | ')}>>`;
|
|
25
|
+
}
|
|
26
|
+
//# sourceMappingURL=expect-type.helper.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"expect-type.helper.js","sourceRoot":"","sources":["../../../src/backend-repositories/generators/expect-type.helper.ts"],"names":[],"mappings":";;AAoBA,gDASC;AAtBD;;;;;;;;;;;;GAYG;AACH,SAAgB,kBAAkB,CAAC,KAAyB;IAC1D,MAAM,cAAc,GAAG,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,MAAM,EAAE,CAAC;SACrD,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,IAAI,KAAK,MAAM,CAAC;SACxC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,IAAI,KAAK,CAAC,IAAI,GAAG,CAAC,CAAA;IAEpC,IAAI,cAAc,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QAChC,OAAO,WAAW,KAAK,CAAC,KAAK,CAAC,IAAI,GAAG,CAAA;IACvC,CAAC;IACD,OAAO,gBAAgB,KAAK,CAAC,KAAK,CAAC,IAAI,KAAK,cAAc,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAA;AAC5E,CAAC"}
|