65 lines
2.6 KiB
JavaScript
65 lines
2.6 KiB
JavaScript
const UnitOfWork = require('../../repositories/UnitOfWork');
|
|
const ContractUploadService = require('../../services/ContractUploadService');
|
|
const { logger } = require('../../middleware/logger'); // <-- import logger
|
|
|
|
class ContractUploadController {
|
|
static async uploadPersonalContract(req, res) {
|
|
const userId = req.user.userId;
|
|
logger.info(`[ContractUploadController] uploadPersonalContract called for userId: ${userId}`);
|
|
const file = req.file;
|
|
// Accept contractData and signatureImage from body (JSON or multipart)
|
|
const contractData = req.body.contractData ? JSON.parse(req.body.contractData) : undefined;
|
|
const signatureImage = req.body.signatureImage; // base64 string or Buffer
|
|
const unitOfWork = new UnitOfWork();
|
|
await unitOfWork.start();
|
|
try {
|
|
const result = await ContractUploadService.uploadContract({
|
|
userId,
|
|
file,
|
|
documentType: 'contract',
|
|
contractCategory: 'personal',
|
|
unitOfWork,
|
|
contractData,
|
|
signatureImage
|
|
});
|
|
await unitOfWork.commit();
|
|
logger.info(`[ContractUploadController] uploadPersonalContract success for userId: ${userId}`);
|
|
res.json({ success: true, upload: result, downloadUrl: result.url || null });
|
|
} catch (error) {
|
|
logger.error(`[ContractUploadController] uploadPersonalContract error for userId: ${userId}`, { error });
|
|
await unitOfWork.rollback(error);
|
|
res.status(400).json({ success: false, message: error.message });
|
|
}
|
|
}
|
|
|
|
static async uploadCompanyContract(req, res) {
|
|
const userId = req.user.userId;
|
|
logger.info(`[ContractUploadController] uploadCompanyContract called for userId: ${userId}`);
|
|
const file = req.file;
|
|
const contractData = req.body.contractData ? JSON.parse(req.body.contractData) : undefined;
|
|
const signatureImage = req.body.signatureImage;
|
|
const unitOfWork = new UnitOfWork();
|
|
await unitOfWork.start();
|
|
try {
|
|
const result = await ContractUploadService.uploadContract({
|
|
userId,
|
|
file,
|
|
documentType: 'contract',
|
|
contractCategory: 'company',
|
|
unitOfWork,
|
|
contractData,
|
|
signatureImage
|
|
});
|
|
await unitOfWork.commit();
|
|
logger.info(`[ContractUploadController] uploadCompanyContract success for userId: ${userId}`);
|
|
res.json({ success: true, upload: result, downloadUrl: result.url || null });
|
|
} catch (error) {
|
|
logger.error(`[ContractUploadController] uploadCompanyContract error for userId: ${userId}`, { error });
|
|
await unitOfWork.rollback(error);
|
|
res.status(400).json({ success: false, message: error.message });
|
|
}
|
|
}
|
|
}
|
|
|
|
module.exports = ContractUploadController;
|