feat: tariff ordering support

This commit is contained in:
skeris 2023-12-15 23:01:09 +03:00
parent e08c9246e2
commit 4355698db8
4 changed files with 9 additions and 1 deletions

@ -31,7 +31,7 @@ export const getTariffs = async (request: GetTariffsRequest): Promise<GetTariffs
const offset = (page - 1) * limit;
const tariffs = await TariffModel.find({ $or: [{ isCustom: false }, { isCustom: true, userId: request.user.id }] })
.sort({ createdAt: "desc" })
.sort({ order: "asc" })
.skip(offset)
.limit(limit)
.lean();
@ -78,6 +78,7 @@ export const createTariff = async (request: CreateTariffRequest, reply: FastifyR
const newTariff = new TariffModel({
name: requestBody.name,
description: requestBody.description,
order: requestBody.order,
price: requestBody.price,
userId: request.user.id,
isCustom: requestBody.isCustom,
@ -134,6 +135,7 @@ export const replaceTariff = async (request: ReplaceTariffRequest, reply: Fastif
});
await tariff.replaceOne({
order: requestBody.order,
name: requestBody.name,
price: requestBody.price,
isCustom: requestBody.isCustom,

@ -18,6 +18,10 @@ const schema: SchemaDefinition<Tariff> = {
type: Number,
required: false,
},
order: {
type: Number,
required: false,
},
userId: {
type: String,
required: true,

@ -8,6 +8,7 @@ export const tariff: SwaggerMessage = {
name: { type: "string" },
description: { type: "string" },
price: { type: "number" },
order: { type: "number" },
isCustom: { type: "boolean" },
privileges: {
type: "array",

@ -6,6 +6,7 @@ export type Tariff = Eloquent & {
userId: string;
description: string;
price?: number;
order?: number;
isCustom: boolean;
privileges: Array<Omit<Privilege, keyof Eloquent>>;
};