89 lines
2.4 KiB
JavaScript
89 lines
2.4 KiB
JavaScript
// src/services/branch.service.js
|
|
const { Branch, Company, User } = require("../models");
|
|
const alertService = require("./alert.service"); // Import alertService
|
|
const { getKoreanSubjectParticle } = require("../utils/koreanParticle"); // Utility for Korean particles
|
|
|
|
class BranchService {
|
|
async createBranch(branchData, user) {
|
|
const branch = await Branch.create(branchData);
|
|
|
|
// Create an alert after creating the branch
|
|
const particle = getKoreanSubjectParticle(branch.name);
|
|
await alertService.createAlert({
|
|
type: "info",
|
|
message: `지점 ${branch.name}${particle} ${user.name}에 의해 생성되었습니다.`,
|
|
companyId: branch.companyId,
|
|
branchId: branch.id,
|
|
});
|
|
|
|
return branch;
|
|
}
|
|
|
|
async findById(id) {
|
|
return await Branch.findByPk(id, {
|
|
include: [
|
|
{ model: Company },
|
|
{
|
|
model: User,
|
|
attributes: { exclude: ["password"] },
|
|
},
|
|
],
|
|
});
|
|
}
|
|
|
|
async findAll(filters = {}) {
|
|
return await Branch.findAll({
|
|
where: filters,
|
|
include: [
|
|
{ model: Company },
|
|
{
|
|
model: User,
|
|
attributes: { exclude: ["password"] },
|
|
},
|
|
],
|
|
});
|
|
}
|
|
|
|
async updateBranch(id, updateData, user) {
|
|
const branch = await Branch.findByPk(id);
|
|
if (!branch) throw new Error("Branch not found");
|
|
|
|
const updatedBranch = await branch.update(updateData);
|
|
|
|
// Create an alert after updating the branch
|
|
const branchName = updateData.name || branch.name;
|
|
const particle = getKoreanSubjectParticle(branchName);
|
|
await alertService.createAlert({
|
|
type: "info",
|
|
message: `지점 ${branchName}${particle} ${user.name}에 의해 업데이트되었습니다.`,
|
|
companyId: branch.companyId,
|
|
branchId: branch.id,
|
|
});
|
|
|
|
return updatedBranch;
|
|
}
|
|
|
|
async deleteBranch(id, user) {
|
|
const branch = await Branch.findByPk(id);
|
|
if (!branch) throw new Error("Branch not found");
|
|
|
|
// Save branch name before deletion for alert message
|
|
const branchName = branch.name;
|
|
const companyId = branch.companyId;
|
|
|
|
await branch.destroy();
|
|
|
|
// Create an alert after deleting the branch
|
|
const particle = getKoreanSubjectParticle(branchName);
|
|
await alertService.createAlert({
|
|
type: "info",
|
|
message: `지점 ${branchName}${particle} ${user.name}에 의해 삭제되었습니다.`,
|
|
companyId: companyId,
|
|
});
|
|
|
|
return true;
|
|
}
|
|
}
|
|
|
|
module.exports = new BranchService();
|