123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
- const HttpException = require('../../utils/HttpException.js');
- const prisma = require('../../prisma/PrismaClient.js');
- const { SearchFilter } = require('../../utils/SearchFilter.js');
- const timeLocal = require('../../utils/TimeLocal.js');
- const { createLog, updateLog, deleteLog } = require('../../utils/LogActivity.js');
- const { formatDateOnly, formatISOWithoutTimezone } = require('../../utils/FormatDate.js');
- const VendorExperienceRepository = require('../../repository/admin/VendorExperienceRepository.js');
- exports.getAllVendorHistoryService = async ({ page, limit, search, sortBy, orderBy }, req) => {
- const skip = (page - 1) * limit;
- const hospitalId = req.params.id;
- const hospital = await prisma.hospital.findFirst({
- where: {
- id: hospitalId
- }
- })
- if (!hospital) {
- throw new HttpException("Hospital not found", 404)
- }
- const where = {
- // ...SearchFilter(search, ['name', 'name_pt']),
- hospital_id: req.params.id,
- deletedAt: null
- };
- const [vendor_histories, total] = await Promise.all([
- VendorExperienceRepository.findAll({ skip, take: limit, where, orderBy: { [sortBy]: orderBy } }),
- VendorExperienceRepository.countAll(where)
- ]);
- return { vendor_histories, total };
- };
- exports.showVendorHistoryService = async (req) => {
- const id_hospital = req.params.id;
- const id_vendor_experience = req.params.id_vendor_experience;
- const hospital = await prisma.hospital.findFirst({
- where: {
- id: id_hospital
- }
- })
- if (!hospital) {
- throw new HttpException("Hospital not found", 404)
- }
- const vendorHistory = await VendorExperienceRepository.findById(id_vendor_experience);
- if (!vendorHistory) {
- throw new HttpException("Vendor history not found", 404);
- }
- return vendorHistory;
- };
- exports.storeVendorHistoryService = async (validateData, req) => {
- const hospitalId = req.params.id;
- const hospital = await prisma.hospital.findFirst({
- where: {
- id: hospitalId
- }
- });
- if (!hospital) {
- throw new HttpException("Hospital not found", 404)
- }
- if (validateData.vendor_id) {
- const vendor = await prisma.vendor.findFirst({
- where: {
- id: validateData.vendor_id
- }
- });
- if (!vendor) {
- throw new HttpException("Vendor not found", 404)
- }
- }
- const SimrsType = ["vendor", "in house", "gratis"];
- if (validateData.simrs_type && !SimrsType.includes(validateData.simrs_type)) {
- throw new HttpException("Simrs type must be vendor, in house, or gratis", 400);
- }
- if (validateData.contract_start_date && validateData.contract_expired_date) {
- if (validateData.contract_start_date >= validateData.contract_expired_date) {
- throw new HttpException("Contract expired date must be after contract date", 400)
- }
- }
- if (validateData.contract_value_min && validateData.contract_value_max) {
- if (validateData.contract_value_min >= validateData.contract_value_max) {
- throw new HttpException("Contract value max must be after contract value min", 400)
- }
- }
- // await prisma.hospital.update({
- // where: { id: hospitalId },
- // data: {
- // simrs_type: validateData.simrs_type
- // }
- // });
- if (validateData.simrs_type) {
- const existingActiveVendors = await prisma.vendorExperience.findMany({
- where: {
- hospital_id: hospitalId,
- status: "active",
- deletedAt: null
- }
- });
- if (existingActiveVendors.length > 0) {
- await prisma.vendorExperience.updateMany({
- where: {
- hospital_id: hospitalId,
- status: "active",
- deletedAt: null
- },
- data: {
- status: "inactive"
- }
- });
- }
- validateData.status = "active";
- }
- const payload = {
- vendor_id: validateData.vendor_id,
- simrs_type: validateData.simrs_type,
- status: validateData.status,
- contract_start_date: validateData.contract_start_date ? new Date(validateData.contract_start_date) : null,
- contract_expired_date: validateData.contract_expired_date ? new Date(validateData.contract_expired_date) : null,
- contract_value_min: validateData.contract_value_min ? Number(validateData.contract_value_min) : null,
- contract_value_max: validateData.contract_value_max ? Number(validateData.contract_value_max) : null,
- positive_notes: validateData.positive_notes,
- negative_notes: validateData.negative_notes,
- hospital_id: hospitalId
- };
- const data = await VendorExperienceRepository.create(payload);
- await createLog(req, data);
- };
- exports.updateVendorHistoryService = async (validateData, req) => {
- const id_hospital = req.params.id;
- const id_vendor_experience = req.params.id_vendor_experience;
- const hospital = await prisma.hospital.findFirst({
- where: {
- id: id_hospital
- }
- })
- if (!hospital) {
- throw new HttpException("Hospital not found", 404)
- }
- const vendorHistory = await VendorExperienceRepository.findById(id_vendor_experience);
- if (!vendorHistory) {
- throw new HttpException("Vendor history not found", 404);
- }
- if (validateData.vendor_id) {
- const existVendor = await prisma.vendor.findFirst({
- where: {
- id: validateData.vendor_id
- }
- });
- if (!existVendor) {
- throw new HttpException("Vendor not found", 404)
- }
- }
- const SimrsType = ["vendor", "in house", "gratis"];
- if (validateData.simrs_type && !SimrsType.includes(validateData.simrs_type)) {
- throw new HttpException("Simrs type must be vendor, in house, or gratis", 400);
- }
- if (
- validateData.simrs_type &&
- vendorHistory.simrs_type === "vendor" &&
- vendorHistory.vendor_id !== null &&
- validateData.simrs_type !== "vendor"
- ) {
- await prisma.vendorExperience.update({
- where: {
- id: id_vendor_experience,
- deletedAt: null
- },
- data: {
- vendor_id: null
- }
- });
- }
- if (validateData.contract_start_date && validateData.contract_expired_date) {
- if (validateData.contract_start_date >= validateData.contract_expired_date) {
- throw new HttpException("Contract expired date must be after contract date", 400)
- }
- }
- if (validateData.contract_value_min && validateData.contract_value_max) {
- if (validateData.contract_value_min >= validateData.contract_value_max) {
- throw new HttpException("Contract value max must be after contract value min", 400)
- }
- }
- if (validateData.status === "active") {
- await prisma.vendorExperience.updateMany({
- where: {
- hospital_id: id_hospital,
- status: "active",
- deletedAt: null,
- NOT: { id: id_vendor_experience },
- },
- data: {
- status: "inactive",
- },
- });
- }
- const payload = {
- status: validateData.status,
- contract_start_date: validateData.contract_start_date ? new Date(validateData.contract_start_date) : vendorHistory.contract_start_date,
- contract_expired_date: validateData.contract_expired_date ? new Date(validateData.contract_expired_date) : vendorHistory.contract_expired_date,
- contract_value_min: validateData.contract_value_min ? Number(validateData.contract_value_min) : vendorHistory.contract_value_min,
- contract_value_max: validateData.contract_value_max ? Number(validateData.contract_value_max) : vendorHistory.contract_value_max,
- positive_notes: validateData.positive_notes,
- negative_notes: validateData.negative_notes,
- simrs_type: validateData.simrs_type,
- vendor_id: validateData.vendor_id,
- };
- const data = await VendorExperienceRepository.update(id_vendor_experience, payload);
- await updateLog(req, data);
- };
- exports.deleteVendorHistoryService = async (req) => {
- const id_hospital = req.params.id;
- const id_vendor_experience = req.params.id_vendor_experience;
- const hospital = await prisma.hospital.findFirst({
- where: {
- id: id_hospital
- }
- })
- if (!hospital) {
- throw new HttpException("Hospital not found", 404)
- }
- const vendor = await VendorExperienceRepository.findById(id_vendor_experience);
- if (!vendor) {
- throw new HttpException("Vendor history not found", 404);
- }
- const data = await VendorExperienceRepository.update(id_vendor_experience, {
- deletedAt: timeLocal.now().toDate()
- });
- await deleteLog(req, data);
- };
|