2023-06-13 15:02:26 +00:00
|
|
|
import { Types } from "mongoose";
|
|
|
|
|
|
|
|
import { TariffModel } from "@/models/tariff.model";
|
|
|
|
|
|
|
|
import { validateEmptyFields } from "@/utils/validate-empty-fields";
|
|
|
|
import { determinePaginationParameters } from "@/utils/determine-pagination-parameters";
|
|
|
|
import { validateTariff } from "./helpers";
|
|
|
|
|
|
|
|
import type { FastifyReply } from "fastify";
|
2023-06-16 16:54:08 +00:00
|
|
|
import type { Tariff } from "@/types/models/tariff.type";
|
2023-06-13 15:02:26 +00:00
|
|
|
import type {
|
|
|
|
CreateTariffRequest,
|
|
|
|
GetTariffRequest,
|
|
|
|
ReplaceTariffRequest,
|
|
|
|
RemoveTariffRequest,
|
|
|
|
GetTariffsResponse,
|
|
|
|
GetTariffsRequest,
|
|
|
|
} from "./types";
|
|
|
|
|
|
|
|
export const getTariffs = async (request: GetTariffsRequest): Promise<GetTariffsResponse> => {
|
|
|
|
const { page, limit } = determinePaginationParameters(request?.query ?? {});
|
|
|
|
|
|
|
|
const tariffsCount = await TariffModel.countDocuments();
|
|
|
|
|
|
|
|
const totalPages = Math.ceil(tariffsCount / limit);
|
|
|
|
const offset = (page - 1) * limit;
|
|
|
|
|
|
|
|
const tariffs = await TariffModel.find({}).sort({ createdAt: "desc" }).skip(offset).limit(limit).lean();
|
|
|
|
|
|
|
|
return { tariffs, totalPages };
|
|
|
|
};
|
|
|
|
|
|
|
|
export const getTariff = async (request: GetTariffRequest, reply: FastifyReply) => {
|
|
|
|
const [requestParams, error] = validateEmptyFields(request.params ?? {}, ["id"]);
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
void reply.status(400);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Types.ObjectId.isValid(requestParams.id)) {
|
|
|
|
void reply.status(400);
|
|
|
|
return new Error("invalid id");
|
|
|
|
}
|
|
|
|
|
|
|
|
const tariff = await TariffModel.findById(requestParams.id).lean();
|
|
|
|
|
|
|
|
if (!tariff) {
|
|
|
|
void reply.status(404);
|
|
|
|
return new Error("tariff not found");
|
|
|
|
}
|
|
|
|
|
|
|
|
return tariff;
|
|
|
|
};
|
|
|
|
|
2023-06-16 16:54:08 +00:00
|
|
|
export const createTariff = async (request: CreateTariffRequest, reply: FastifyReply): Promise<Tariff> => {
|
2023-06-13 15:02:26 +00:00
|
|
|
const [requestBody, error] = validateTariff(request.body);
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
void reply.status(400);
|
2023-06-16 16:54:08 +00:00
|
|
|
throw error;
|
2023-06-13 15:02:26 +00:00
|
|
|
}
|
2023-06-16 16:54:08 +00:00
|
|
|
|
2023-06-13 15:52:32 +00:00
|
|
|
for (const privilege of requestBody.privilegies) {
|
|
|
|
if (!Types.ObjectId.isValid(privilege.privilegeId)) {
|
|
|
|
void reply.status(404);
|
2023-06-16 16:54:08 +00:00
|
|
|
throw new Error(`privilege id <${privilege.privilegeId}> invalid`);
|
2023-06-13 15:52:32 +00:00
|
|
|
}
|
|
|
|
}
|
2023-06-13 15:02:26 +00:00
|
|
|
|
|
|
|
const newTariff = new TariffModel({
|
|
|
|
name: requestBody.name,
|
|
|
|
price: requestBody.price,
|
|
|
|
isCustom: requestBody.isCustom,
|
2023-06-16 16:54:08 +00:00
|
|
|
privilegies: requestBody.privilegies,
|
2023-06-13 15:02:26 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
await newTariff.save();
|
|
|
|
|
|
|
|
return newTariff;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const replaceTariff = async (request: ReplaceTariffRequest, reply: FastifyReply) => {
|
|
|
|
const [requestBody, error] = validateTariff(request.body ?? {});
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
void reply.status(400);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Types.ObjectId.isValid(request.params?.id || "")) {
|
|
|
|
void reply.status(400);
|
|
|
|
return new Error("invalid id");
|
|
|
|
}
|
|
|
|
|
|
|
|
const tariff = await TariffModel.findById(request.params?.id);
|
|
|
|
|
|
|
|
if (!tariff) {
|
|
|
|
void reply.status(404);
|
|
|
|
return new Error("tariff not found");
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const privilege of requestBody.privilegies) {
|
|
|
|
if (!Types.ObjectId.isValid(privilege.privilegeId)) {
|
|
|
|
void reply.status(404);
|
|
|
|
return new Error(`privilege id <${privilege.privilegeId}> invalid`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
await tariff.replaceOne({
|
|
|
|
name: requestBody.name,
|
|
|
|
price: requestBody.price,
|
|
|
|
isCustom: requestBody.isCustom,
|
2023-06-16 16:54:08 +00:00
|
|
|
privilegies: requestBody.privilegies,
|
2023-06-13 15:02:26 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return tariff;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const removeTariff = async (request: RemoveTariffRequest, reply: FastifyReply) => {
|
|
|
|
const [{ id }, error] = validateEmptyFields(request.body ?? {}, ["id"]);
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
void reply.status(400);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Types.ObjectId.isValid(id)) {
|
|
|
|
void reply.status(400);
|
|
|
|
return new Error("invalid id");
|
|
|
|
}
|
|
|
|
|
|
|
|
const tariff = await TariffModel.findByIdAndUpdate(id, {
|
|
|
|
$set: { isDeleted: true, deletedAt: new Date() },
|
|
|
|
}).lean();
|
|
|
|
|
|
|
|
if (!tariff) {
|
|
|
|
void reply.status(404);
|
|
|
|
return new Error("tariff not found");
|
|
|
|
}
|
|
|
|
|
|
|
|
return tariff;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const deleteTariff = async (request: RemoveTariffRequest, reply: FastifyReply) => {
|
|
|
|
const [{ id }, error] = validateEmptyFields(request.body ?? {}, ["id"]);
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
void reply.status(400);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Types.ObjectId.isValid(id)) {
|
|
|
|
void reply.status(400);
|
|
|
|
return new Error("invalid id");
|
|
|
|
}
|
|
|
|
|
|
|
|
const tariff = await TariffModel.findByIdAndDelete(id).lean();
|
|
|
|
|
|
|
|
if (!tariff) {
|
|
|
|
void reply.status(404);
|
|
|
|
return new Error("tariff not found");
|
|
|
|
}
|
|
|
|
|
|
|
|
return tariff;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const restoreTariff = async (request: RemoveTariffRequest, reply: FastifyReply) => {
|
|
|
|
const [{ id }, error] = validateEmptyFields(request.body ?? {}, ["id"]);
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
void reply.status(400);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Types.ObjectId.isValid(id)) {
|
|
|
|
void reply.status(400);
|
|
|
|
return new Error("invalid id");
|
|
|
|
}
|
|
|
|
|
|
|
|
const tariff = await TariffModel.findByIdAndUpdate(id, { $set: { isDeleted: false } }).lean();
|
|
|
|
|
|
|
|
if (!tariff) {
|
|
|
|
void reply.status(404);
|
|
|
|
return new Error("tariff not found");
|
|
|
|
}
|
|
|
|
|
|
|
|
return tariff;
|
|
|
|
};
|