139 lines
3.2 KiB
JavaScript
139 lines
3.2 KiB
JavaScript
const prisma = require('../../../helpers/db/db_connection');
|
|
|
|
class CertificateRepository {
|
|
async create(certificateData) {
|
|
return prisma.certificate.create({
|
|
data: certificateData,
|
|
include: this.certificateDetailInclude(),
|
|
});
|
|
}
|
|
|
|
async findById(id) {
|
|
return prisma.certificate.findUnique({
|
|
where: { id: parseInt(id) },
|
|
include: this.certificateDetailInclude(),
|
|
});
|
|
}
|
|
|
|
async findByUserAndProject(id_user, id_project) {
|
|
return prisma.certificate.findUnique({
|
|
where: {
|
|
id_project_id_user: {
|
|
id_project: parseInt(id_project),
|
|
id_user: parseInt(id_user),
|
|
},
|
|
},
|
|
include: this.certificateDetailInclude(),
|
|
});
|
|
}
|
|
|
|
async findUserCertificates(id_user) {
|
|
return prisma.certificate.findMany({
|
|
where: { id_user: parseInt(id_user) },
|
|
include: this.certificateDetailInclude(),
|
|
orderBy: { issued_at: 'desc' },
|
|
});
|
|
}
|
|
|
|
async findProjectCertificates(id_project) {
|
|
return prisma.certificate.findMany({
|
|
where: { id_project: parseInt(id_project) },
|
|
include: this.certificateDetailInclude(),
|
|
orderBy: { issued_at: 'desc' },
|
|
});
|
|
}
|
|
|
|
async findAll() {
|
|
return prisma.certificate.findMany({
|
|
include: this.certificateDetailInclude(),
|
|
orderBy: { issued_at: 'desc' },
|
|
});
|
|
}
|
|
|
|
async findByCertificateNo(certificate_no) {
|
|
return prisma.certificate.findUnique({
|
|
where: { certificate_no },
|
|
include: this.certificateDetailInclude(),
|
|
});
|
|
}
|
|
|
|
async findByUuid(uuid) {
|
|
return prisma.certificate.findUnique({
|
|
where: { uuid },
|
|
include: this.certificateDetailInclude(),
|
|
});
|
|
}
|
|
|
|
async getProjectTasksAndSubmissions(id_project, id_user) {
|
|
return prisma.project.findUnique({
|
|
where: { id: parseInt(id_project) },
|
|
select: {
|
|
id: true,
|
|
project_name: true,
|
|
tasks: {
|
|
select: {
|
|
id: true,
|
|
submissions: {
|
|
where: { id_user: parseInt(id_user) },
|
|
select: {
|
|
id: true,
|
|
submitted_at: true,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
members: {
|
|
where: {
|
|
id_user: parseInt(id_user),
|
|
status: 'active',
|
|
},
|
|
select: {
|
|
id: true,
|
|
},
|
|
},
|
|
},
|
|
});
|
|
}
|
|
|
|
async findProjectById(id_project) {
|
|
return prisma.project.findUnique({
|
|
where: { id: parseInt(id_project) },
|
|
});
|
|
}
|
|
|
|
async countByProject(id_project) {
|
|
return prisma.certificate.count({
|
|
where: { id_project: parseInt(id_project) },
|
|
});
|
|
}
|
|
|
|
async updateProjectTemplate(id_project, templatePath) {
|
|
return prisma.project.update({
|
|
where: { id: parseInt(id_project) },
|
|
data: { certificate_template: templatePath },
|
|
});
|
|
}
|
|
|
|
certificateDetailInclude() {
|
|
return {
|
|
user: {
|
|
select: {
|
|
id: true,
|
|
full_name: true,
|
|
email: true,
|
|
},
|
|
},
|
|
project: {
|
|
select: {
|
|
id: true,
|
|
project_name: true,
|
|
description: true,
|
|
certificate_template: true,
|
|
},
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
module.exports = new CertificateRepository();
|