Update file index.ts
This commit is contained in:
parent
87f253bbb9
commit
a46fd883b4
@ -1,230 +1,230 @@
|
|||||||
import { Types } from "mongoose";
|
import { Types } from "mongoose";
|
||||||
|
|
||||||
import { TariffModel } from "@/models/tariff.model";
|
import { TariffModel } from "@/models/tariff.model";
|
||||||
import { PrivilegeModel } from "@/models/privilege.model";
|
import { PrivilegeModel } from "@/models/privilege.model";
|
||||||
|
|
||||||
import { validateEmptyFields } from "@/utils/validate-empty-fields";
|
import { validateEmptyFields } from "@/utils/validate-empty-fields";
|
||||||
import { determinePaginationParameters } from "@/utils/determine-pagination-parameters";
|
import { determinePaginationParameters } from "@/utils/determine-pagination-parameters";
|
||||||
import { validateTariff } from "./helpers";
|
import { validateTariff } from "./helpers";
|
||||||
|
|
||||||
import type { FastifyReply } from "fastify";
|
import type { FastifyReply } from "fastify";
|
||||||
import type { Privilege } from "@/types/models/privilege.type";
|
import type { Privilege } from "@/types/models/privilege.type";
|
||||||
import type { Tariff } from "@/types/models/tariff.type";
|
import type { Tariff } from "@/types/models/tariff.type";
|
||||||
import type { Eloquent } from "@/types/models/eloquent.type";
|
import type { Eloquent } from "@/types/models/eloquent.type";
|
||||||
import type {
|
import type {
|
||||||
CreateTariffRequest,
|
CreateTariffRequest,
|
||||||
GetTariffRequest,
|
GetTariffRequest,
|
||||||
ReplaceTariffRequest,
|
ReplaceTariffRequest,
|
||||||
RemoveTariffRequest,
|
RemoveTariffRequest,
|
||||||
GetTariffsResponse,
|
GetTariffsResponse,
|
||||||
GetTariffsRequest,
|
GetTariffsRequest,
|
||||||
} from "./types";
|
} from "./types";
|
||||||
|
|
||||||
export const getTariffs = async (request: GetTariffsRequest): Promise<GetTariffsResponse> => {
|
export const getTariffs = async (request: GetTariffsRequest): Promise<GetTariffsResponse> => {
|
||||||
const { page, limit } = determinePaginationParameters(request?.query ?? {});
|
const { page, limit } = determinePaginationParameters(request?.query ?? {});
|
||||||
|
|
||||||
const tariffsCount = await TariffModel.countDocuments();
|
const tariffsCount = await TariffModel.countDocuments();
|
||||||
|
|
||||||
const totalPages = Math.ceil(tariffsCount / limit);
|
const totalPages = Math.ceil(tariffsCount / limit);
|
||||||
const offset = (page - 1) * limit;
|
const offset = (page - 1) * limit;
|
||||||
|
|
||||||
const tariffs = await TariffModel.find({}).sort({ createdAt: "desc" }).skip(offset).limit(limit).lean();
|
const tariffs = await TariffModel.find({}).sort({ createdAt: "desc" }).skip(offset).limit(limit).lean();
|
||||||
|
|
||||||
return { tariffs, totalPages };
|
return { tariffs, totalPages };
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getTariff = async (request: GetTariffRequest, reply: FastifyReply) => {
|
export const getTariff = async (request: GetTariffRequest, reply: FastifyReply) => {
|
||||||
const [requestParams, error] = validateEmptyFields(request.params ?? {}, ["id"]);
|
const [requestParams, error] = validateEmptyFields(request.params ?? {}, ["id"]);
|
||||||
|
|
||||||
if (error) {
|
if (error) {
|
||||||
void reply.status(400);
|
void reply.status(400);
|
||||||
return error;
|
return error;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!Types.ObjectId.isValid(requestParams.id)) {
|
if (!Types.ObjectId.isValid(requestParams.id)) {
|
||||||
void reply.status(400);
|
void reply.status(400);
|
||||||
return new Error("invalid id");
|
return new Error("invalid id");
|
||||||
}
|
}
|
||||||
|
|
||||||
const tariff = await TariffModel.findById(requestParams.id).lean();
|
const tariff = await TariffModel.findById(requestParams.id).lean();
|
||||||
|
|
||||||
if (!tariff) {
|
if (!tariff) {
|
||||||
void reply.status(404);
|
void reply.status(404);
|
||||||
return new Error("tariff not found");
|
return new Error("tariff not found");
|
||||||
}
|
}
|
||||||
|
|
||||||
return tariff;
|
return tariff;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const createTariff = async (request: CreateTariffRequest, reply: FastifyReply): Promise<Tariff> => {
|
export const createTariff = async (request: CreateTariffRequest, reply: FastifyReply): Promise<Tariff> => {
|
||||||
const [requestBody, error] = validateTariff(request.body);
|
const [requestBody, error] = validateTariff(request.body);
|
||||||
|
|
||||||
if (error) {
|
if (error) {
|
||||||
void reply.status(400);
|
void reply.status(400);
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const privilege of requestBody.privilegies) {
|
for (const privilege of requestBody.privilegies) {
|
||||||
if (!Types.ObjectId.isValid(privilege.privilegeId)) {
|
if (!Types.ObjectId.isValid(privilege.privilegeId)) {
|
||||||
void reply.status(404);
|
void reply.status(404);
|
||||||
throw new Error(`privilege id <${privilege.privilegeId}> invalid`);
|
throw new Error(`privilege id <${privilege.privilegeId}> invalid`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const privilegiesMap = new Map(requestBody.privilegies.map((privilege) => [privilege.privilegeId, privilege]));
|
const privilegiesMap = new Map(requestBody.privilegies.map((privilege) => [privilege.privilegeId, privilege]));
|
||||||
const privilegeIDs = requestBody.privilegies.map(({ privilegeId }) => privilegeId);
|
const privilegeIDs = requestBody.privilegies.map(({ privilegeId }) => privilegeId);
|
||||||
const privilegies = await PrivilegeModel.find({ privilegeId: privilegeIDs }).lean();
|
const privilegies = await PrivilegeModel.find({ _id: privilegeIDs }).lean();
|
||||||
|
|
||||||
const cleanPrivilegies = privilegies.map<Omit<Privilege, keyof Eloquent>>((privilege) => {
|
const cleanPrivilegies = privilegies.map<Omit<Privilege, keyof Eloquent>>((privilege) => {
|
||||||
const currentPrivilege = privilegiesMap.get(privilege.privilegeId);
|
const currentPrivilege = privilegiesMap.get(privilege.privilegeId);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
name: privilege.name,
|
name: privilege.name,
|
||||||
privilegeId: privilege.privilegeId,
|
privilegeId: privilege.privilegeId,
|
||||||
serviceKey: privilege.serviceKey,
|
serviceKey: privilege.serviceKey,
|
||||||
description: privilege.description,
|
description: privilege.description,
|
||||||
amount: currentPrivilege?.amount ?? 0,
|
amount: currentPrivilege?.amount ?? 0,
|
||||||
type: privilege.type,
|
type: privilege.type,
|
||||||
value: privilege.value,
|
value: privilege.value,
|
||||||
price: privilege.price,
|
price: privilege.price,
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
const newTariff = new TariffModel({
|
const newTariff = new TariffModel({
|
||||||
name: requestBody.name,
|
name: requestBody.name,
|
||||||
price: requestBody.price,
|
price: requestBody.price,
|
||||||
isCustom: requestBody.isCustom,
|
isCustom: requestBody.isCustom,
|
||||||
privilegies: cleanPrivilegies,
|
privilegies: cleanPrivilegies,
|
||||||
});
|
});
|
||||||
|
|
||||||
await newTariff.save();
|
await newTariff.save();
|
||||||
|
|
||||||
return newTariff;
|
return newTariff;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const replaceTariff = async (request: ReplaceTariffRequest, reply: FastifyReply) => {
|
export const replaceTariff = async (request: ReplaceTariffRequest, reply: FastifyReply) => {
|
||||||
const [requestBody, error] = validateTariff(request.body ?? {});
|
const [requestBody, error] = validateTariff(request.body ?? {});
|
||||||
|
|
||||||
if (error) {
|
if (error) {
|
||||||
void reply.status(400);
|
void reply.status(400);
|
||||||
return error;
|
return error;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!Types.ObjectId.isValid(request.params?.id || "")) {
|
if (!Types.ObjectId.isValid(request.params?.id || "")) {
|
||||||
void reply.status(400);
|
void reply.status(400);
|
||||||
return new Error("invalid id");
|
return new Error("invalid id");
|
||||||
}
|
}
|
||||||
|
|
||||||
const tariff = await TariffModel.findById(request.params?.id);
|
const tariff = await TariffModel.findById(request.params?.id);
|
||||||
|
|
||||||
if (!tariff) {
|
if (!tariff) {
|
||||||
void reply.status(404);
|
void reply.status(404);
|
||||||
return new Error("tariff not found");
|
return new Error("tariff not found");
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const privilege of requestBody.privilegies) {
|
for (const privilege of requestBody.privilegies) {
|
||||||
if (!Types.ObjectId.isValid(privilege.privilegeId)) {
|
if (!Types.ObjectId.isValid(privilege.privilegeId)) {
|
||||||
void reply.status(404);
|
void reply.status(404);
|
||||||
return new Error(`privilege id <${privilege.privilegeId}> invalid`);
|
return new Error(`privilege id <${privilege.privilegeId}> invalid`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const privilegiesMap = new Map(requestBody.privilegies.map((privilege) => [privilege.privilegeId, privilege]));
|
const privilegiesMap = new Map(requestBody.privilegies.map((privilege) => [privilege.privilegeId, privilege]));
|
||||||
const privilegeIDs = requestBody.privilegies.map(({ privilegeId }) => privilegeId);
|
const privilegeIDs = requestBody.privilegies.map(({ privilegeId }) => privilegeId);
|
||||||
const privilegies = await PrivilegeModel.find({ privilegeId: privilegeIDs }).lean();
|
const privilegies = await PrivilegeModel.find({ _id: privilegeIDs }).lean();
|
||||||
|
|
||||||
const cleanPrivilegies = privilegies.map<Omit<Privilege, keyof Eloquent>>((privilege) => {
|
const cleanPrivilegies = privilegies.map<Omit<Privilege, keyof Eloquent>>((privilege) => {
|
||||||
const currentPrivilege = privilegiesMap.get(privilege.privilegeId);
|
const currentPrivilege = privilegiesMap.get(privilege.privilegeId);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
name: privilege.name,
|
name: privilege.name,
|
||||||
privilegeId: privilege.privilegeId,
|
privilegeId: privilege.privilegeId,
|
||||||
serviceKey: privilege.serviceKey,
|
serviceKey: privilege.serviceKey,
|
||||||
description: privilege.description,
|
description: privilege.description,
|
||||||
amount: currentPrivilege?.amount ?? 0,
|
amount: currentPrivilege?.amount ?? 0,
|
||||||
type: privilege.type,
|
type: privilege.type,
|
||||||
value: privilege.value,
|
value: privilege.value,
|
||||||
price: privilege.price,
|
price: privilege.price,
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
await tariff.replaceOne({
|
await tariff.replaceOne({
|
||||||
name: requestBody.name,
|
name: requestBody.name,
|
||||||
price: requestBody.price,
|
price: requestBody.price,
|
||||||
isCustom: requestBody.isCustom,
|
isCustom: requestBody.isCustom,
|
||||||
privilegies: cleanPrivilegies,
|
privilegies: cleanPrivilegies,
|
||||||
});
|
});
|
||||||
|
|
||||||
return tariff;
|
return tariff;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const removeTariff = async (request: RemoveTariffRequest, reply: FastifyReply) => {
|
export const removeTariff = async (request: RemoveTariffRequest, reply: FastifyReply) => {
|
||||||
const [{ id }, error] = validateEmptyFields(request.body ?? {}, ["id"]);
|
const [{ id }, error] = validateEmptyFields(request.body ?? {}, ["id"]);
|
||||||
|
|
||||||
if (error) {
|
if (error) {
|
||||||
void reply.status(400);
|
void reply.status(400);
|
||||||
return error;
|
return error;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!Types.ObjectId.isValid(id)) {
|
if (!Types.ObjectId.isValid(id)) {
|
||||||
void reply.status(400);
|
void reply.status(400);
|
||||||
return new Error("invalid id");
|
return new Error("invalid id");
|
||||||
}
|
}
|
||||||
|
|
||||||
const tariff = await TariffModel.findByIdAndUpdate(id, {
|
const tariff = await TariffModel.findByIdAndUpdate(id, {
|
||||||
$set: { isDeleted: true, deletedAt: new Date() },
|
$set: { isDeleted: true, deletedAt: new Date() },
|
||||||
}).lean();
|
}).lean();
|
||||||
|
|
||||||
if (!tariff) {
|
if (!tariff) {
|
||||||
void reply.status(404);
|
void reply.status(404);
|
||||||
return new Error("tariff not found");
|
return new Error("tariff not found");
|
||||||
}
|
}
|
||||||
|
|
||||||
return tariff;
|
return tariff;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const deleteTariff = async (request: RemoveTariffRequest, reply: FastifyReply) => {
|
export const deleteTariff = async (request: RemoveTariffRequest, reply: FastifyReply) => {
|
||||||
const [{ id }, error] = validateEmptyFields(request.body ?? {}, ["id"]);
|
const [{ id }, error] = validateEmptyFields(request.body ?? {}, ["id"]);
|
||||||
|
|
||||||
if (error) {
|
if (error) {
|
||||||
void reply.status(400);
|
void reply.status(400);
|
||||||
return error;
|
return error;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!Types.ObjectId.isValid(id)) {
|
if (!Types.ObjectId.isValid(id)) {
|
||||||
void reply.status(400);
|
void reply.status(400);
|
||||||
return new Error("invalid id");
|
return new Error("invalid id");
|
||||||
}
|
}
|
||||||
|
|
||||||
const tariff = await TariffModel.findByIdAndDelete(id).lean();
|
const tariff = await TariffModel.findByIdAndDelete(id).lean();
|
||||||
|
|
||||||
if (!tariff) {
|
if (!tariff) {
|
||||||
void reply.status(404);
|
void reply.status(404);
|
||||||
return new Error("tariff not found");
|
return new Error("tariff not found");
|
||||||
}
|
}
|
||||||
|
|
||||||
return tariff;
|
return tariff;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const restoreTariff = async (request: RemoveTariffRequest, reply: FastifyReply) => {
|
export const restoreTariff = async (request: RemoveTariffRequest, reply: FastifyReply) => {
|
||||||
const [{ id }, error] = validateEmptyFields(request.body ?? {}, ["id"]);
|
const [{ id }, error] = validateEmptyFields(request.body ?? {}, ["id"]);
|
||||||
|
|
||||||
if (error) {
|
if (error) {
|
||||||
void reply.status(400);
|
void reply.status(400);
|
||||||
return error;
|
return error;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!Types.ObjectId.isValid(id)) {
|
if (!Types.ObjectId.isValid(id)) {
|
||||||
void reply.status(400);
|
void reply.status(400);
|
||||||
return new Error("invalid id");
|
return new Error("invalid id");
|
||||||
}
|
}
|
||||||
|
|
||||||
const tariff = await TariffModel.findByIdAndUpdate(id, { $set: { isDeleted: false } }).lean();
|
const tariff = await TariffModel.findByIdAndUpdate(id, { $set: { isDeleted: false } }).lean();
|
||||||
|
|
||||||
if (!tariff) {
|
if (!tariff) {
|
||||||
void reply.status(404);
|
void reply.status(404);
|
||||||
return new Error("tariff not found");
|
return new Error("tariff not found");
|
||||||
}
|
}
|
||||||
|
|
||||||
return tariff;
|
return tariff;
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user