Admin Page work - Still blank/broken
This commit is contained in:
@@ -536,4 +536,103 @@ export class CatalogController {
|
||||
reply.code(500).send({ error: 'Failed to retrieve change logs' });
|
||||
}
|
||||
}
|
||||
|
||||
// BULK DELETE ENDPOINT
|
||||
|
||||
async bulkDeleteCatalogEntity(
|
||||
request: FastifyRequest<{ Params: { entity: string }; Body: { ids: number[] } }>,
|
||||
reply: FastifyReply
|
||||
): Promise<void> {
|
||||
try {
|
||||
const { entity } = request.params;
|
||||
const { ids } = request.body;
|
||||
const actorId = request.userContext?.userId || 'unknown';
|
||||
|
||||
// Validate entity type
|
||||
const validEntities = ['makes', 'models', 'years', 'trims', 'engines'];
|
||||
if (!validEntities.includes(entity)) {
|
||||
reply.code(400).send({
|
||||
error: 'Invalid entity type',
|
||||
message: `Entity must be one of: ${validEntities.join(', ')}`
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
// Validate IDs are provided
|
||||
if (!ids || !Array.isArray(ids) || ids.length === 0) {
|
||||
reply.code(400).send({
|
||||
error: 'Invalid request',
|
||||
message: 'At least one ID must be provided'
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
// Validate all IDs are valid integers
|
||||
const invalidIds = ids.filter(id => !Number.isInteger(id) || id <= 0);
|
||||
if (invalidIds.length > 0) {
|
||||
reply.code(400).send({
|
||||
error: 'Invalid IDs',
|
||||
message: 'All IDs must be positive integers'
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const deleted: number[] = [];
|
||||
const failed: Array<{ id: number; error: string }> = [];
|
||||
|
||||
// Map entity to delete method
|
||||
const deleteMethodMap: Record<string, (id: number, actorId: string) => Promise<void>> = {
|
||||
makes: (id, actor) => this.catalogService.deleteMake(id, actor),
|
||||
models: (id, actor) => this.catalogService.deleteModel(id, actor),
|
||||
years: (id, actor) => this.catalogService.deleteYear(id, actor),
|
||||
trims: (id, actor) => this.catalogService.deleteTrim(id, actor),
|
||||
engines: (id, actor) => this.catalogService.deleteEngine(id, actor)
|
||||
};
|
||||
|
||||
const deleteMethod = deleteMethodMap[entity];
|
||||
|
||||
// Process each deletion sequentially to maintain data consistency
|
||||
for (const id of ids) {
|
||||
try {
|
||||
await deleteMethod(id, actorId);
|
||||
deleted.push(id);
|
||||
} catch (error: any) {
|
||||
logger.error(`Error deleting ${entity} in bulk operation`, {
|
||||
error: error.message,
|
||||
entity,
|
||||
id,
|
||||
actorId
|
||||
});
|
||||
|
||||
failed.push({
|
||||
id,
|
||||
error: error.message || `Failed to delete ${entity}`
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const response = {
|
||||
deleted,
|
||||
failed
|
||||
};
|
||||
|
||||
// Return 207 Multi-Status if there were any failures, 204 if all succeeded
|
||||
if (failed.length > 0) {
|
||||
reply.code(207).send(response);
|
||||
} else {
|
||||
reply.code(204).send();
|
||||
}
|
||||
} catch (error: any) {
|
||||
logger.error('Error in bulk delete catalog entity', {
|
||||
error: error.message,
|
||||
entity: request.params.entity,
|
||||
actorId: request.userContext?.userId
|
||||
});
|
||||
|
||||
reply.code(500).send({
|
||||
error: 'Internal server error',
|
||||
message: 'Failed to process bulk deletion'
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user