Files
hounam-submit-form-backend/src/modules/forms/center/service/center.service.ts
2026-05-26 16:00:09 +03:30

52 lines
1.6 KiB
TypeScript

import createHttpError from "http-errors";
import { Controller } from "../../../../core/controller/main.controller";
import { GlobalErrorMessages } from "../../../../core/messages/errors";
import { Center } from "../../../../models/Center";
import { JobCategory } from "../../../../models/JobCategory";
class CenterServiceClass extends Controller {
// ایجاد مرکز جدید
async create(data: any) {
return await Center.create(data);
}
// دریافت لیست تمام مراکز (همراه با رسته‌های شغلی مرتبط)
async getAll() {
return await Center.findAll({
include: [{ model: JobCategory, as: "categories" }],
order: [["createdAt", "DESC"]],
});
}
// دریافت یک مرکز خاص با ID
async getById(id: string) {
const center = await Center.findByPk(id, {
include: [{ model: JobCategory, as: "categories" }],
});
if (!center) throw new Error("مرکز مورد نظر یافت نشد.");
return center;
}
// بروزرسانی اطلاعات مرکز
async update(id: string, data: any) {
const center = await this.getById(id);
return await center.update(data);
}
// حذف مرکز
async delete(id: string) {
const center = await this.getById(id);
await center.destroy();
return { message: "مرکز با موفقیت حذف شد." };
}
// متد کمکی برای گرفتن مراکز فوری (Urgent)
async getUrgentCenters() {
return await Center.findAll({ where: { isUrgent: true } });
}
}
const CenterService = new CenterServiceClass();
export default CenterService;