feat: Nambahin template certificate endpoint
This commit is contained in:
@@ -199,6 +199,47 @@ class CertificateService {
|
||||
}
|
||||
}
|
||||
|
||||
async uploadCertificateTemplate(id_project, file, actor) {
|
||||
try {
|
||||
if (!id_project) {
|
||||
return error(new BadRequestError('Project ID is required'));
|
||||
}
|
||||
|
||||
if (!file) {
|
||||
return error(new BadRequestError('No file uploaded'));
|
||||
}
|
||||
|
||||
if (!actor?.id || !actor?.role) {
|
||||
return error(new ForbiddenError('Access denied: authentication data is missing'));
|
||||
}
|
||||
|
||||
const project = await certificateRepository.findProjectById(id_project);
|
||||
if (!project) {
|
||||
return error(new NotFoundError('Project not found'));
|
||||
}
|
||||
|
||||
// Authorization checks:
|
||||
if (actor.role === 'mentor') {
|
||||
if (project.id_admin !== parseInt(actor.id)) {
|
||||
return error(new ForbiddenError('Access denied: you are not the mentor of this project'));
|
||||
}
|
||||
} else if (actor.role !== 'admin') {
|
||||
return error(new ForbiddenError('Access denied: only admins and mentors can upload certificate templates'));
|
||||
}
|
||||
|
||||
const templatePath = `/uploads/${file.filename}`;
|
||||
|
||||
await certificateRepository.updateProjectTemplate(id_project, templatePath);
|
||||
|
||||
return data({
|
||||
id_project: parseInt(id_project),
|
||||
certificate_template: templatePath,
|
||||
});
|
||||
} catch (err) {
|
||||
return error(err);
|
||||
}
|
||||
}
|
||||
|
||||
mapCertificate(cert) {
|
||||
if (!cert) return null;
|
||||
return {
|
||||
@@ -217,6 +258,7 @@ class CertificateService {
|
||||
id: cert.project.id,
|
||||
project_name: cert.project.project_name,
|
||||
description: cert.project.description,
|
||||
certificate_template: cert.project.certificate_template,
|
||||
} : null,
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user