add edit & delete button to tender & update contacts dashbaord
This commit is contained in:
@@ -27,6 +27,20 @@ export class TendersController {
|
||||
}
|
||||
}
|
||||
|
||||
async delete(req: AuthRequest, res: Response, next: NextFunction) {
|
||||
try {
|
||||
await tendersService.delete(req.params.id, req.user!.id)
|
||||
res.json(
|
||||
ResponseFormatter.success(
|
||||
true,
|
||||
'تم حذف المناقصة بنجاح - Tender deleted successfully'
|
||||
)
|
||||
)
|
||||
} catch (error) {
|
||||
next(error)
|
||||
}
|
||||
}
|
||||
|
||||
async findAll(req: AuthRequest, res: Response, next: NextFunction) {
|
||||
try {
|
||||
const page = parseInt(req.query.page as string) || 1;
|
||||
|
||||
@@ -112,6 +112,14 @@ router.put(
|
||||
tendersController.update
|
||||
);
|
||||
|
||||
router.delete(
|
||||
'/:id',
|
||||
authorize('tenders', 'tenders', 'delete'),
|
||||
param('id').isUUID(),
|
||||
validate,
|
||||
tendersController.delete
|
||||
);
|
||||
|
||||
// Tender history
|
||||
router.get(
|
||||
'/:id/history',
|
||||
|
||||
@@ -125,6 +125,62 @@ class TendersService {
|
||||
}
|
||||
}
|
||||
|
||||
async delete(id: string, userId: string) {
|
||||
const tender = await prisma.tender.findUnique({
|
||||
where: { id },
|
||||
include: {
|
||||
attachments: true,
|
||||
directives: {
|
||||
include: {
|
||||
attachments: true,
|
||||
},
|
||||
},
|
||||
convertedDeal: {
|
||||
select: { id: true },
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (!tender) {
|
||||
throw new AppError(404, 'Tender not found');
|
||||
}
|
||||
|
||||
if (tender.convertedDeal) {
|
||||
throw new AppError(400, 'Cannot delete tender that has been converted to deal');
|
||||
}
|
||||
|
||||
for (const attachment of tender.attachments || []) {
|
||||
if (attachment.path && fs.existsSync(attachment.path)) {
|
||||
fs.unlinkSync(attachment.path);
|
||||
}
|
||||
}
|
||||
|
||||
for (const directive of tender.directives || []) {
|
||||
for (const attachment of directive.attachments || []) {
|
||||
if (attachment.path && fs.existsSync(attachment.path)) {
|
||||
fs.unlinkSync(attachment.path);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
await prisma.tender.delete({
|
||||
where: { id },
|
||||
});
|
||||
|
||||
await AuditLogger.log({
|
||||
entityType: 'TENDER',
|
||||
entityId: id,
|
||||
action: 'DELETE',
|
||||
userId,
|
||||
changes: {
|
||||
deletedTenderNumber: tender.tenderNumber,
|
||||
deletedTitle: tender.title,
|
||||
},
|
||||
});
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private buildTenderNotes(
|
||||
plainNotes?: string | null,
|
||||
extra?: {
|
||||
|
||||
Reference in New Issue
Block a user