feat: private tariff & remove duplicate bag
This commit is contained in:
parent
08e15f5108
commit
c3fb5177bc
@ -1,6 +1,6 @@
|
||||
import type { FastifyRequest, FastifyReply, HookHandlerDoneFunction as Done } from "fastify";
|
||||
import type { FastifyRequest, FastifyReply } from "fastify";
|
||||
|
||||
export const verifyUser = async (request: FastifyRequest, reply: FastifyReply, done: Done) => {
|
||||
export const verifyUser = async (request: FastifyRequest, reply: FastifyReply) => {
|
||||
try {
|
||||
const { id } = await request.jwtVerify<{ id?: string }>();
|
||||
|
||||
@ -14,6 +14,4 @@ export const verifyUser = async (request: FastifyRequest, reply: FastifyReply, d
|
||||
void reply.status(401);
|
||||
return reply.send(nativeError);
|
||||
}
|
||||
|
||||
done();
|
||||
};
|
||||
|
@ -23,114 +23,100 @@ import type {
|
||||
export const getTariffs = async (request: GetTariffsRequest): Promise<GetTariffsResponse> => {
|
||||
const { page, limit } = determinePaginationParameters(request?.query ?? {});
|
||||
|
||||
const tariffsCount = await TariffModel.countDocuments();
|
||||
const tariffsCount = await TariffModel.countDocuments({
|
||||
$or: [{ isCustom: false }, { isCustom: true, userId: request.user.id }],
|
||||
});
|
||||
|
||||
const totalPages = Math.ceil(tariffsCount / limit);
|
||||
const offset = (page - 1) * limit;
|
||||
|
||||
const tariffs = await TariffModel.find({}).sort({ createdAt: "desc" }).skip(offset).limit(limit).lean();
|
||||
const tariffs = await TariffModel.find({ $or: [{ isCustom: false }, { isCustom: true, userId: request.user.id }] })
|
||||
.sort({ createdAt: "desc" })
|
||||
.skip(offset)
|
||||
.limit(limit)
|
||||
.lean();
|
||||
|
||||
return { tariffs, totalPages };
|
||||
};
|
||||
|
||||
export const getTariff = async (request: GetTariffRequest, reply: FastifyReply) => {
|
||||
export const getTariff = async (request: GetTariffRequest, reply: FastifyReply): Promise<Tariff> => {
|
||||
const [requestParams, error] = validateEmptyFields(request.params ?? {}, ["id"]);
|
||||
|
||||
if (error) {
|
||||
void reply.status(400);
|
||||
return error;
|
||||
throw error;
|
||||
}
|
||||
|
||||
if (!Types.ObjectId.isValid(requestParams.id)) {
|
||||
void reply.status(400);
|
||||
return new Error("invalid id");
|
||||
throw new Error("invalid id");
|
||||
}
|
||||
|
||||
const tariff = await TariffModel.findById(requestParams.id).lean();
|
||||
const tariff = await TariffModel.findOne({
|
||||
$or: [
|
||||
{ _id: new Types.ObjectId(requestParams.id), isCustom: false },
|
||||
{ _id: new Types.ObjectId(requestParams.id), isCustom: true, userId: request.user.id },
|
||||
],
|
||||
}).lean();
|
||||
|
||||
if (!tariff) {
|
||||
void reply.status(404);
|
||||
return new Error("tariff not found");
|
||||
throw new Error("tariff not found");
|
||||
}
|
||||
|
||||
return tariff;
|
||||
};
|
||||
|
||||
export const createTariff = async (request: CreateTariffRequest, reply: FastifyReply) => {
|
||||
export const createTariff = async (request: CreateTariffRequest, reply: FastifyReply): Promise<Tariff> => {
|
||||
const [requestBody, error] = validateTariff(request.body);
|
||||
|
||||
if (error) {
|
||||
void reply.status(400);
|
||||
console.info("this 1");
|
||||
return error;
|
||||
throw error;
|
||||
}
|
||||
|
||||
for (const privilege of requestBody.privilegies) {
|
||||
if (!Types.ObjectId.isValid(privilege.privilegeId)) {
|
||||
void reply.status(404);
|
||||
console.info("this 2");
|
||||
return new Error(`privilege id <${privilege.privilegeId}> invalid`);
|
||||
throw new Error(`privilege id <${privilege.privilegeId}> invalid`);
|
||||
}
|
||||
}
|
||||
console.info("this 3");
|
||||
|
||||
const privilegiesMap = new Map(requestBody.privilegies.map((privilege) => [privilege.privilegeId, privilege]));
|
||||
const privilegeIDs = requestBody.privilegies.map(({ privilegeId }) => privilegeId);
|
||||
PrivilegeModel.find({ _id: privilegeIDs }).lean().then(privilegies => {
|
||||
console.info("this 4", privilegies);
|
||||
|
||||
const cleanPrivilegies = privilegies.map<Omit<Privilege, keyof Eloquent>>((privilege) => {
|
||||
const currentPrivilege = privilegiesMap.get(privilege._id.toString());
|
||||
|
||||
return {
|
||||
name: privilege.name,
|
||||
privilegeId: privilege.privilegeId,
|
||||
serviceKey: privilege.serviceKey,
|
||||
description: privilege.description,
|
||||
amount: currentPrivilege?.amount ?? 0,
|
||||
type: privilege.type,
|
||||
value: privilege.value,
|
||||
price: privilege.price,
|
||||
};
|
||||
});
|
||||
|
||||
const newTariff = new TariffModel({
|
||||
name: requestBody.name,
|
||||
price: requestBody.price,
|
||||
isCustom: requestBody.isCustom,
|
||||
privilegies: requestBody.privilegies,
|
||||
});
|
||||
console.info("this 5");
|
||||
|
||||
newTariff.save();
|
||||
const newTariff = new TariffModel({
|
||||
name: requestBody.name,
|
||||
price: requestBody.price,
|
||||
userId: request.user.id,
|
||||
isCustom: requestBody.isCustom,
|
||||
privilegies: requestBody.privilegies,
|
||||
});
|
||||
|
||||
|
||||
return newTariff.save();
|
||||
};
|
||||
|
||||
export const replaceTariff = async (request: ReplaceTariffRequest, reply: FastifyReply) => {
|
||||
export const replaceTariff = async (request: ReplaceTariffRequest, reply: FastifyReply): Promise<Tariff> => {
|
||||
const [requestBody, error] = validateTariff(request.body ?? {});
|
||||
|
||||
if (error) {
|
||||
void reply.status(400);
|
||||
return error;
|
||||
throw error;
|
||||
}
|
||||
|
||||
if (!Types.ObjectId.isValid(request.params?.id || "")) {
|
||||
void reply.status(400);
|
||||
return new Error("invalid id");
|
||||
throw new Error("invalid id");
|
||||
}
|
||||
|
||||
const tariff = await TariffModel.findById(request.params?.id);
|
||||
|
||||
if (!tariff) {
|
||||
void reply.status(404);
|
||||
return new Error("tariff not found");
|
||||
throw 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`);
|
||||
throw new Error(`privilege id <${privilege.privilegeId}> invalid`);
|
||||
}
|
||||
}
|
||||
|
||||
@ -163,17 +149,17 @@ export const replaceTariff = async (request: ReplaceTariffRequest, reply: Fastif
|
||||
return tariff;
|
||||
};
|
||||
|
||||
export const removeTariff = async (request: RemoveTariffRequest, reply: FastifyReply) => {
|
||||
export const removeTariff = async (request: RemoveTariffRequest, reply: FastifyReply): Promise<Tariff> => {
|
||||
const [{ id }, error] = validateEmptyFields(request.body ?? {}, ["id"]);
|
||||
|
||||
if (error) {
|
||||
void reply.status(400);
|
||||
return error;
|
||||
throw error;
|
||||
}
|
||||
|
||||
if (!Types.ObjectId.isValid(id)) {
|
||||
void reply.status(400);
|
||||
return new Error("invalid id");
|
||||
throw new Error("invalid id");
|
||||
}
|
||||
|
||||
const tariff = await TariffModel.findByIdAndUpdate(id, {
|
||||
@ -182,53 +168,53 @@ export const removeTariff = async (request: RemoveTariffRequest, reply: FastifyR
|
||||
|
||||
if (!tariff) {
|
||||
void reply.status(404);
|
||||
return new Error("tariff not found");
|
||||
throw new Error("tariff not found");
|
||||
}
|
||||
|
||||
return tariff;
|
||||
};
|
||||
|
||||
export const deleteTariff = async (request: RemoveTariffRequest, reply: FastifyReply) => {
|
||||
export const deleteTariff = async (request: RemoveTariffRequest, reply: FastifyReply): Promise<Tariff> => {
|
||||
const [{ id }, error] = validateEmptyFields(request.body ?? {}, ["id"]);
|
||||
|
||||
if (error) {
|
||||
void reply.status(400);
|
||||
return error;
|
||||
throw error;
|
||||
}
|
||||
|
||||
if (!Types.ObjectId.isValid(id)) {
|
||||
void reply.status(400);
|
||||
return new Error("invalid id");
|
||||
throw new Error("invalid id");
|
||||
}
|
||||
|
||||
const tariff = await TariffModel.findByIdAndDelete(id).lean();
|
||||
|
||||
if (!tariff) {
|
||||
void reply.status(404);
|
||||
return new Error("tariff not found");
|
||||
throw new Error("tariff not found");
|
||||
}
|
||||
|
||||
return tariff;
|
||||
};
|
||||
|
||||
export const restoreTariff = async (request: RemoveTariffRequest, reply: FastifyReply) => {
|
||||
export const restoreTariff = async (request: RemoveTariffRequest, reply: FastifyReply): Promise<Tariff> => {
|
||||
const [{ id }, error] = validateEmptyFields(request.body ?? {}, ["id"]);
|
||||
|
||||
if (error) {
|
||||
void reply.status(400);
|
||||
return error;
|
||||
throw error;
|
||||
}
|
||||
|
||||
if (!Types.ObjectId.isValid(id)) {
|
||||
void reply.status(400);
|
||||
return new Error("invalid id");
|
||||
throw 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");
|
||||
throw new Error("tariff not found");
|
||||
}
|
||||
|
||||
return tariff;
|
||||
|
@ -14,6 +14,10 @@ const schema: SchemaDefinition<Tariff> = {
|
||||
type: Number,
|
||||
required: false,
|
||||
},
|
||||
userId: {
|
||||
type: String,
|
||||
required: true,
|
||||
},
|
||||
isCustom: {
|
||||
type: Boolean,
|
||||
default: false,
|
||||
|
@ -22,8 +22,8 @@ import {
|
||||
} from "@/swagger/tariff";
|
||||
|
||||
export const setTariffRoutes = (router: Router): void => {
|
||||
router.get("/", getTariffs, { schema: getTariffsSchema });
|
||||
router.get("/:id", getTariff, { schema: getTariffSchema });
|
||||
router.get("/", getTariffs, { preHandler: [verifyUser], schema: getTariffsSchema });
|
||||
router.get("/:id", getTariff, { preHandler: [verifyUser], schema: getTariffSchema });
|
||||
router.post("/", createTariff, { preHandler: [verifyUser], schema: createTariffsSchema });
|
||||
router.post("/restore", restoreTariff, { preHandler: [verifyUser], schema: restoreTariffsSchema });
|
||||
router.put("/:id", replaceTariff, { preHandler: [verifyUser], schema: replaceTariffsSchema });
|
||||
|
@ -1,4 +1,4 @@
|
||||
import type { Eloquent } from "../models/eloquent.type";
|
||||
import type { Tariff } from "../models/tariff.type";
|
||||
|
||||
export type TariffMessage = Partial<Omit<Tariff, keyof Eloquent>>;
|
||||
export type TariffMessage = Partial<Omit<Tariff, keyof Eloquent | "userId">>;
|
||||
|
@ -3,6 +3,7 @@ import type { Eloquent } from "./eloquent.type";
|
||||
|
||||
export type Tariff = Eloquent & {
|
||||
name: string;
|
||||
userId: string;
|
||||
price?: number;
|
||||
isCustom: boolean;
|
||||
privilegies: Array<Omit<Privilege, keyof Eloquent>>;
|
||||
|
Loading…
Reference in New Issue
Block a user