Skip to content

Commit

Permalink
Refactor payout log pagination in customer module
Browse files Browse the repository at this point in the history
 • Implement new customer-payout-log.function.ts for payout log pagination
 • Create CustomerPayoutLogControllerPaginate with schema and handler
 • Fix incorrect import in payout-log/paginate.spec.ts
 • Update CustomerPayoutLogServicePaginate to include type enhancements and correct aggregation logic
  • Loading branch information
jamalsoueidan committed Mar 31, 2024
1 parent 8a05818 commit a34d045
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 13 deletions.
12 changes: 12 additions & 0 deletions src/functions/customer-payout-log.function.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import "module-alias/register";

import { app } from "@azure/functions";

import { CustomerPayoutLogControllerPaginate } from "./customer/controllers/payout-log/paginate";

app.http("customerPayoutLogPaginate", {
methods: ["GET"],
authLevel: "anonymous",
route: "customer/{customerId?}/payout-log/{payoutId}",
handler: CustomerPayoutLogControllerPaginate,
});
33 changes: 33 additions & 0 deletions src/functions/customer/controllers/payout-log/paginate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { _ } from "~/library/handler";

import { z } from "zod";
import { NumberOrStringType, ObjectIdType } from "~/library/zod";
import { CustomerPayoutLogServicePaginate } from "../../services/payout-log/paginate";

export type CustomerPayoutLogControllerPaginateRequest = {
query: z.infer<typeof CustomerPayoutLogControllerPaginateSchema>;
};

enum SortOrder {
ASC = "asc",
DESC = "desc",
}

export const CustomerPayoutLogControllerPaginateSchema = z.object({
page: NumberOrStringType,
limit: NumberOrStringType.optional(),
sortOrder: z.nativeEnum(SortOrder).optional(),
customerId: NumberOrStringType,
payoutId: ObjectIdType,
});

export type CustomerPayoutLogControllerPaginateResponse = Awaited<
ReturnType<typeof CustomerPayoutLogServicePaginate>
>;

export const CustomerPayoutLogControllerPaginate = _(
async ({ query }: CustomerPayoutLogControllerPaginateRequest) => {
const validateData = CustomerPayoutLogControllerPaginateSchema.parse(query);
return CustomerPayoutLogServicePaginate(validateData);
}
);
4 changes: 2 additions & 2 deletions src/functions/customer/services/payout-log/paginate.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { createShipping } from "~/library/jest/helpers/shipping";

import { CustomerPayoutServiceCreate } from "../payout/create";
import { dummyDataBalance } from "../payout/fixtures/dummydata.balance";
import { CustomerServicePayoutLogPaginate } from "./paginate";
import { CustomerPayoutLogServicePaginate } from "./paginate";

require("~/library/jest/mongoose/mongodb.jest");

Expand Down Expand Up @@ -46,7 +46,7 @@ describe("CustomerServicePayoutLogList", () => {
it("should get payout with line-items", async () => {
const payout = await CustomerPayoutServiceCreate({ customerId });

const response = await CustomerServicePayoutLogPaginate({
const response = await CustomerPayoutLogServicePaginate({
page: 1,
customerId,
payoutId: payout._id,
Expand Down
28 changes: 17 additions & 11 deletions src/functions/customer/services/payout-log/paginate.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,31 @@
import { FilterQuery } from "mongoose";
import { OrderModel } from "~/functions/order/order.models";
import { OrderLineItem } from "~/functions/order/order.types";
import {
IPayoutLogDocument,
PayoutLog,
PayoutLogModel,
PayoutLogReferenceType,
} from "~/functions/payout-log";
import { ShippingModel } from "~/functions/shipping/shipping.model";
import { Shipping } from "~/functions/shipping/shipping.types";
import { StringOrObjectId } from "~/library/zod";

export type CustomerServicePayoutLogPaginateProps = {
export type CustomerPayoutLogServicePaginateProps = {
page: number;
customerId: number;
payoutId: number;
payoutId: StringOrObjectId;
limit?: number;
sortOrder?: "asc" | "desc";
};

export const CustomerServicePayoutLogPaginate = async ({
export const CustomerPayoutLogServicePaginate = async ({
page = 1,
limit = 10,
sortOrder = "desc",
customerId,
payoutId,
}: CustomerServicePayoutLogPaginateProps) => {
}: CustomerPayoutLogServicePaginateProps) => {
let query: FilterQuery<IPayoutLogDocument> = {
customerId,
payout: payoutId,
Expand All @@ -33,7 +37,9 @@ export const CustomerServicePayoutLogPaginate = async ({
const totalCount = await PayoutLogModel.countDocuments(query);
const totalPages = Math.ceil(totalCount / limit);

const logs = await PayoutLogModel.aggregate([
const logs = await PayoutLogModel.aggregate<
PayoutLog & { referenceDocument: OrderLineItem | Shipping }
>([
{ $match: query },
{ $sort: { createdAt: sortParam } },
{ $skip: skipDocuments },
Expand Down Expand Up @@ -61,7 +67,7 @@ export const CustomerServicePayoutLogPaginate = async ({
{ $project: { line_item: "$line_items", _id: 0 } },
{ $limit: 1 },
],
as: "relatedDocument",
as: "referenceDocument",
},
},
],
Expand All @@ -72,7 +78,7 @@ export const CustomerServicePayoutLogPaginate = async ({
from: ShippingModel.collection.name,
localField: "referenceId",
foreignField: "_id",
as: "relatedDocument",
as: "referenceDocument",
},
},
],
Expand All @@ -87,17 +93,17 @@ export const CustomerServicePayoutLogPaginate = async ({
{ $replaceRoot: { newRoot: "$results" } },
{
$unwind: {
path: "$relatedDocument",
path: "$referenceDocument",
preserveNullAndEmptyArrays: true,
},
},
{
$addFields: {
relatedDocument: {
referenceDocument: {
$cond: {
if: { $eq: ["$referenceType", PayoutLogReferenceType.LINE_ITEM] },
then: "$relatedDocument.line_item",
else: "$relatedDocument",
then: "$referenceDocument.line_item",
else: "$referenceDocument",
},
},
},
Expand Down

0 comments on commit a34d045

Please sign in to comment.