forked from wangziqi/gongxue-base
refactor(deposits): remove refund approval remnants
This commit is contained in:
@@ -0,0 +1,101 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { getDataSourceToken } from '@nestjs/typeorm';
|
||||
import { DatabaseMigrationsService } from './database-migrations.service';
|
||||
|
||||
function createRunner(columns: string[]) {
|
||||
return {
|
||||
connect: jest.fn(),
|
||||
release: jest.fn(),
|
||||
query: jest.fn().mockResolvedValue([]),
|
||||
getTables: jest.fn().mockResolvedValue(columns.length ? [{ name: 'deposits' }] : []),
|
||||
getTable: jest.fn().mockResolvedValue({
|
||||
name: 'deposits',
|
||||
columns: columns.map((name) => ({ name })),
|
||||
}),
|
||||
renameColumn: jest.fn().mockResolvedValue(undefined),
|
||||
dropColumn: jest.fn().mockResolvedValue(undefined),
|
||||
};
|
||||
}
|
||||
|
||||
async function createService(runner: ReturnType<typeof createRunner>) {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
providers: [
|
||||
DatabaseMigrationsService,
|
||||
{
|
||||
provide: getDataSourceToken(),
|
||||
useValue: {
|
||||
options: { type: 'better-sqlite3' },
|
||||
createQueryRunner: jest.fn().mockReturnValue(runner),
|
||||
},
|
||||
},
|
||||
],
|
||||
}).compile();
|
||||
return module.get(DatabaseMigrationsService) as DatabaseMigrationsService & {
|
||||
cleanupDepositRefundColumns(): Promise<void>;
|
||||
};
|
||||
}
|
||||
|
||||
describe('DatabaseMigrationsService — deposit refund cleanup', () => {
|
||||
it('renames refund audit fields and drops approval-flow remnants', async () => {
|
||||
const runner = createRunner([
|
||||
'id',
|
||||
'refund_status',
|
||||
'refund_requested_at',
|
||||
'refund_approved_by',
|
||||
'refund_approved_at',
|
||||
'refund_rejected_reason',
|
||||
]);
|
||||
const service = await createService(runner);
|
||||
|
||||
await service.cleanupDepositRefundColumns();
|
||||
|
||||
expect(runner.renameColumn).toHaveBeenCalledWith(
|
||||
'deposits',
|
||||
'refund_approved_by',
|
||||
'refunded_by',
|
||||
);
|
||||
expect(runner.renameColumn).toHaveBeenCalledWith(
|
||||
'deposits',
|
||||
'refund_approved_at',
|
||||
'refunded_at',
|
||||
);
|
||||
expect(runner.dropColumn).toHaveBeenCalledWith('deposits', 'refund_status');
|
||||
expect(runner.dropColumn).toHaveBeenCalledWith('deposits', 'refund_requested_at');
|
||||
expect(runner.dropColumn).toHaveBeenCalledWith('deposits', 'refund_rejected_reason');
|
||||
expect(runner.release).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('merges legacy audit values before dropping duplicate legacy columns', async () => {
|
||||
const runner = createRunner([
|
||||
'id',
|
||||
'refund_approved_by',
|
||||
'refund_approved_at',
|
||||
'refunded_by',
|
||||
'refunded_at',
|
||||
]);
|
||||
const service = await createService(runner);
|
||||
|
||||
await service.cleanupDepositRefundColumns();
|
||||
|
||||
expect(runner.query).toHaveBeenCalledWith(
|
||||
'UPDATE deposits SET refunded_by = COALESCE(refunded_by, refund_approved_by)',
|
||||
);
|
||||
expect(runner.query).toHaveBeenCalledWith(
|
||||
'UPDATE deposits SET refunded_at = COALESCE(refunded_at, refund_approved_at)',
|
||||
);
|
||||
expect(runner.dropColumn).toHaveBeenCalledWith('deposits', 'refund_approved_by');
|
||||
expect(runner.dropColumn).toHaveBeenCalledWith('deposits', 'refund_approved_at');
|
||||
expect(runner.renameColumn).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('does nothing when the deposits table is absent', async () => {
|
||||
const runner = createRunner([]);
|
||||
const service = await createService(runner);
|
||||
|
||||
await service.cleanupDepositRefundColumns();
|
||||
|
||||
expect(runner.renameColumn).not.toHaveBeenCalled();
|
||||
expect(runner.dropColumn).not.toHaveBeenCalled();
|
||||
expect(runner.release).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -15,6 +15,7 @@ export class DatabaseMigrationsService implements OnApplicationBootstrap {
|
||||
await this.normalizeClassDates();
|
||||
await this.protectAttendanceHistory();
|
||||
await this.removeUnusedClassroomColumns();
|
||||
await this.cleanupDepositRefundColumns();
|
||||
}
|
||||
|
||||
private async removeUnusedClassroomColumns(): Promise<void> {
|
||||
@@ -36,6 +37,44 @@ export class DatabaseMigrationsService implements OnApplicationBootstrap {
|
||||
}
|
||||
}
|
||||
|
||||
private async cleanupDepositRefundColumns(): Promise<void> {
|
||||
const runner = this.dataSource.createQueryRunner();
|
||||
await runner.connect();
|
||||
try {
|
||||
const tables = await runner.getTables(['deposits']);
|
||||
if (tables.length === 0) return;
|
||||
|
||||
const table = await runner.getTable('deposits');
|
||||
const columnNames = new Set(table?.columns.map((column) => column.name) ?? []);
|
||||
for (const [legacyName, currentName] of [
|
||||
['refund_approved_by', 'refunded_by'],
|
||||
['refund_approved_at', 'refunded_at'],
|
||||
] as const) {
|
||||
if (!columnNames.has(legacyName)) continue;
|
||||
|
||||
if (columnNames.has(currentName)) {
|
||||
await runner.query(
|
||||
`UPDATE deposits SET ${currentName} = COALESCE(${currentName}, ${legacyName})`,
|
||||
);
|
||||
await runner.dropColumn('deposits', legacyName);
|
||||
} else {
|
||||
await runner.renameColumn('deposits', legacyName, currentName);
|
||||
columnNames.add(currentName);
|
||||
}
|
||||
columnNames.delete(legacyName);
|
||||
}
|
||||
|
||||
for (const columnName of ['refund_status', 'refund_requested_at', 'refund_rejected_reason']) {
|
||||
if (columnNames.has(columnName)) {
|
||||
await runner.dropColumn('deposits', columnName);
|
||||
columnNames.delete(columnName);
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
await runner.release();
|
||||
}
|
||||
}
|
||||
|
||||
private async ensureAiConfigTable(): Promise<void> {
|
||||
const runner = this.dataSource.createQueryRunner();
|
||||
await runner.connect();
|
||||
|
||||
Reference in New Issue
Block a user