Skip to content

Commit 870a47b

Browse files
committed
feat: update SlugUuidParameter and refactor batchId handling in TableSchemaChangeEntity
1 parent 641a368 commit 870a47b

6 files changed

Lines changed: 22 additions & 15 deletions

File tree

backend/src/decorators/slug-uuid.decorator.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ export type SlugUuidParameter =
1515
| 'companyId'
1616
| 'threadId'
1717
| 'filterId'
18-
| 'chatId';
18+
| 'chatId'
19+
| 'changeId'
20+
| 'batchId';
1921
export const SlugUuid = createParamDecorator(
2022
(parameterName: SlugUuidParameter = 'slug', ctx: ExecutionContext): string => {
2123
const request: IRequestWithCognitoInfo = ctx.switchToHttp().getRequest();
@@ -32,6 +34,8 @@ export const SlugUuid = createParamDecorator(
3234
'threadId',
3335
'filterId',
3436
'chatId',
37+
'changeId',
38+
'batchId',
3539
];
3640
if (!availableSlagParameters.includes(parameterName)) {
3741
throw new BadRequestException(Messages.UUID_INVALID);

backend/src/entities/table-schema/table-schema-change.entity.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export class TableSchemaChangeEntity {
1515
@Column({ type: 'varchar', length: 38 })
1616
connectionId: string;
1717

18-
@Column({ type: 'varchar', length: 38, nullable: true })
18+
@Column({ type: 'uuid', nullable: true })
1919
batchId: string | null;
2020

2121
@Column({ type: 'int', default: 0 })

backend/src/entities/table-schema/table-schema.controller.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
HttpStatus,
77
Inject,
88
Injectable,
9-
Param,
109
Post,
1110
Query,
1211
UseGuards,
@@ -101,7 +100,7 @@ export class TableSchemaController {
101100
@Post('/table-schema/change/:changeId/approve')
102101
@HttpCode(HttpStatus.OK)
103102
async approve(
104-
@Param('changeId') changeId: string,
103+
@SlugUuid('changeId') changeId: string,
105104
@UserId() userId: string,
106105
@MasterPassword() masterPassword: string,
107106
@Body() body: ApproveSchemaChangeDto,
@@ -121,7 +120,7 @@ export class TableSchemaController {
121120
@UseGuards(SchemaChangeOwnershipGuard)
122121
@Post('/table-schema/change/:changeId/reject')
123122
@HttpCode(HttpStatus.OK)
124-
async reject(@Param('changeId') changeId: string, @UserId() userId: string): Promise<SchemaChangeResponseDto> {
123+
async reject(@SlugUuid('changeId') changeId: string, @UserId() userId: string): Promise<SchemaChangeResponseDto> {
125124
return await this.rejectSchemaChangeUseCase.execute({ changeId, userId });
126125
}
127126

@@ -132,7 +131,7 @@ export class TableSchemaController {
132131
@Post('/table-schema/change/:changeId/rollback')
133132
@HttpCode(HttpStatus.OK)
134133
async rollback(
135-
@Param('changeId') changeId: string,
134+
@SlugUuid('changeId') changeId: string,
136135
@UserId() userId: string,
137136
@MasterPassword() masterPassword: string,
138137
): Promise<SchemaChangeResponseDto> {
@@ -162,7 +161,7 @@ export class TableSchemaController {
162161
@ApiResponse({ status: 200, type: SchemaChangeResponseDto })
163162
@UseGuards(SchemaChangeOwnershipGuard)
164163
@Get('/table-schema/change/:changeId')
165-
async get(@Param('changeId') changeId: string, @UserId() userId: string): Promise<SchemaChangeResponseDto> {
164+
async get(@SlugUuid('changeId') changeId: string, @UserId() userId: string): Promise<SchemaChangeResponseDto> {
166165
return await this.getSchemaChangeUseCase.execute({ changeId, userId });
167166
}
168167

@@ -177,7 +176,7 @@ export class TableSchemaController {
177176
@Post('/table-schema/batch/:batchId/approve')
178177
@HttpCode(HttpStatus.OK)
179178
async approveBatch(
180-
@Param('batchId') batchId: string,
179+
@SlugUuid('batchId') batchId: string,
181180
@UserId() userId: string,
182181
@MasterPassword() masterPassword: string,
183182
@Body() body: ApproveBatchSchemaChangeDto,
@@ -197,7 +196,7 @@ export class TableSchemaController {
197196
@Post('/table-schema/batch/:batchId/reject')
198197
@HttpCode(HttpStatus.OK)
199198
async rejectBatch(
200-
@Param('batchId') batchId: string,
199+
@SlugUuid('batchId') batchId: string,
201200
@UserId() userId: string,
202201
): Promise<SchemaChangeBatchResponseDto> {
203202
return await this.rejectBatchSchemaChangeUseCase.execute({ batchId, userId });
@@ -210,7 +209,7 @@ export class TableSchemaController {
210209
@Post('/table-schema/batch/:batchId/rollback')
211210
@HttpCode(HttpStatus.OK)
212211
async rollbackBatch(
213-
@Param('batchId') batchId: string,
212+
@SlugUuid('batchId') batchId: string,
214213
@UserId() userId: string,
215214
@MasterPassword() masterPassword: string,
216215
): Promise<SchemaChangeBatchResponseDto> {
@@ -222,7 +221,10 @@ export class TableSchemaController {
222221
@ApiResponse({ status: 200, type: SchemaChangeBatchResponseDto })
223222
@UseGuards(SchemaChangeBatchOwnershipGuard)
224223
@Get('/table-schema/batch/:batchId')
225-
async getBatch(@Param('batchId') batchId: string, @UserId() userId: string): Promise<SchemaChangeBatchResponseDto> {
224+
async getBatch(
225+
@SlugUuid('batchId') batchId: string,
226+
@UserId() userId: string,
227+
): Promise<SchemaChangeBatchResponseDto> {
226228
return await this.getBatchSchemaChangeUseCase.execute({ batchId, userId });
227229
}
228230
}

backend/src/entities/table-schema/use-cases/generate-schema-change.use-case.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { BadRequestException, Inject, Injectable, Logger, NotFoundException, Scope } from '@nestjs/common';
22
import { getDataAccessObject } from '@rocketadmin/shared-code/dist/src/data-access-layer/shared/create-data-access-object.js';
33
import { ConnectionTypesEnum } from '@rocketadmin/shared-code/dist/src/shared/enums/connection-types-enum.js';
4-
import { nanoid } from 'nanoid';
4+
import crypto from 'crypto';
55
import { AICoreService, AIProviderType, MessageBuilder } from '../../../ai-core/index.js';
66
import AbstractUseCase from '../../../common/abstract-use.case.js';
77
import { IGlobalDatabaseContext } from '../../../common/application/global-database-context.interface.js';
@@ -115,7 +115,7 @@ export class GenerateSchemaChangeUseCase
115115
const aiModelUsed =
116116
this.aiCoreService.getAvailableProviders().find((p) => p.type === this.provider)?.defaultModel ?? null;
117117

118-
const batchId = nanoid(12);
118+
const batchId = crypto.randomUUID();
119119
const items: Partial<TableSchemaChangeEntity>[] = proposals.map((proposal, index) => ({
120120
connectionId,
121121
batchId,

backend/src/entities/table-schema/utils/schema-change-batch-ownership.guard.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { InjectRepository } from '@nestjs/typeorm';
1010
import { Repository } from 'typeorm';
1111
import { IRequestWithCognitoInfo } from '../../../authorization/index.js';
1212
import { Messages } from '../../../exceptions/text/messages.js';
13+
import { ValidationHelper } from '../../../helpers/validators/validation-helper.js';
1314
import { CedarAction } from '../../cedar-authorization/cedar-action-map.js';
1415
import { CedarAuthorizationService } from '../../cedar-authorization/cedar-authorization.service.js';
1516
import { TableSchemaChangeEntity } from '../table-schema-change.entity.js';
@@ -27,7 +28,7 @@ export class SchemaChangeBatchOwnershipGuard implements CanActivate {
2728
const userId = request.decoded.sub;
2829
const batchId: string = request.params?.batchId;
2930

30-
if (!batchId || batchId.length > 64) {
31+
if (!batchId || !ValidationHelper.isValidUUID(batchId)) {
3132
throw new BadRequestException('Invalid or missing batchId.');
3233
}
3334

backend/src/migrations/1777295364595-AddBatchColumnsToTableSchemaChange.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export class AddBatchColumnsToTableSchemaChange1777295364595 implements Migratio
44
name = 'AddBatchColumnsToTableSchemaChange1777295364595';
55

66
public async up(queryRunner: QueryRunner): Promise<void> {
7-
await queryRunner.query(`ALTER TABLE "table_schema_change" ADD "batchId" character varying(38)`);
7+
await queryRunner.query(`ALTER TABLE "table_schema_change" ADD "batchId" uuid`);
88
await queryRunner.query(`ALTER TABLE "table_schema_change" ADD "orderInBatch" integer NOT NULL DEFAULT '0'`);
99
await queryRunner.query(`CREATE INDEX "IDX_tsc_batch_order" ON "table_schema_change" ("batchId", "orderInBatch") `);
1010
}

0 commit comments

Comments
 (0)