Skip to content

Commit c0d7069

Browse files
Merge pull request #125 from jamalsoueidan/payout-log-update
Add orderId and createdAt to Payout Log schema and aggregation logic
2 parents 3ef530c + 61b33fb commit c0d7069

File tree

6 files changed

+66
-22
lines changed

6 files changed

+66
-22
lines changed

openapi/paths/customer/payout-log/_types/payout-log.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ properties:
44
type: string
55
customerId:
66
type: number
7+
orderId:
8+
type: number
9+
orderCreatedAt:
10+
type: string
711
referenceType:
812
$ref: "../_types/payout-log-type.yaml"
913
referenceId:
@@ -20,6 +24,8 @@ properties:
2024
required:
2125
- _id
2226
- customerId
27+
- orderId
28+
- orderCreatedAt
2329
- referenceType
2430
- referenceId
2531
- referenceDocument

src/functions/customer/services/payout-log/paginate.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,12 @@ export const CustomerPayoutLogServicePaginate = async ({
6464
$expr: { $eq: ["$line_items.id", "$$lineItemId"] },
6565
},
6666
},
67-
{ $project: { line_item: "$line_items", _id: 0 } },
67+
{
68+
$project: {
69+
line_item: "$line_items",
70+
_id: 0,
71+
},
72+
},
6873
{ $limit: 1 },
6974
],
7075
as: "referenceDocument",

src/functions/customer/services/payout/balance.spec.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ describe("CustomerPayoutServiceBalance", () => {
5353
await PayoutLogModel.create({
5454
customerId,
5555
referenceId: shipping._id,
56+
orderId: dummyDataBalance.id,
5657
referenceType: PayoutLogReferenceType.SHIPPING,
5758
payout: new mongoose.Types.ObjectId(),
5859
});
@@ -70,6 +71,7 @@ describe("CustomerPayoutServiceBalance", () => {
7071
await PayoutLogModel.create({
7172
customerId,
7273
referenceId: dumbData.line_items[0].id,
74+
orderId: dumbData.id,
7375
referenceType: PayoutLogReferenceType.LINE_ITEM,
7476
payout: new mongoose.Types.ObjectId(),
7577
});
@@ -92,6 +94,7 @@ describe("CustomerPayoutServiceBalance", () => {
9294
await PayoutLogModel.create({
9395
customerId,
9496
referenceId: dumbData.line_items[0].id,
97+
orderId: dumbData.id,
9598
referenceType: PayoutLogReferenceType.LINE_ITEM,
9699
payout: new mongoose.Types.ObjectId(),
97100
});

src/functions/customer/services/payout/create.ts

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import { OrderModel } from "~/functions/order/order.models";
22
import { OrderLineItem } from "~/functions/order/order.types";
33
import { PayoutModel, PayoutStatus } from "~/functions/payout";
4-
import { PayoutLogModel, PayoutLogReferenceType } from "~/functions/payout-log";
4+
import {
5+
PayoutLog,
6+
PayoutLogModel,
7+
PayoutLogReferenceType,
8+
} from "~/functions/payout-log";
59
import { Shipping } from "~/functions/shipping/shipping.types";
610
import { NotFoundError } from "~/library/handler";
711
import { CustomerPayoutAccountServiceGet } from "../payout-account/get";
@@ -14,11 +18,11 @@ export type CustomerPayoutServiceCreateProps = {
1418
export const CustomerPayoutServiceCreate = async ({
1519
customerId,
1620
}: CustomerPayoutServiceCreateProps) => {
17-
const lineItems = await CustomerPayoutServiceGetLineItemsFulfilled({
21+
const orders = await CustomerPayoutServiceGetLineItemsFulfilled({
1822
customerId,
1923
});
2024

21-
if (lineItems.length === 0) {
25+
if (orders.length === 0) {
2226
throw new NotFoundError([
2327
{
2428
code: "custom",
@@ -39,23 +43,27 @@ export const CustomerPayoutServiceCreate = async ({
3943
]);
4044
}
4145

42-
const totalLineItems = lineItems.reduce(
46+
const totalLineItems = orders.reduce(
4347
(accumulator, { line_items }) => accumulator + parseFloat(line_items.price),
4448
0
4549
);
4650

47-
const shippings = lineItems
51+
const shippings = orders
4852
.filter((lineItem) => lineItem.shipping)
49-
.map(({ shipping }) => shipping);
53+
.map(({ id, created_at, shipping }) => ({
54+
id,
55+
created_at,
56+
shipping,
57+
}));
5058

5159
let uniqueShippings = Array.from(
5260
new Map(
53-
shippings.map((shipping) => [shipping._id.toString(), shipping])
61+
shippings.map((shipping) => [shipping.shipping._id.toString(), shipping])
5462
).values()
5563
);
5664

5765
const totalShippingAmount = uniqueShippings.reduce(
58-
(accumulator, { cost }) => accumulator + cost.value,
66+
(accumulator, { shipping }) => accumulator + shipping.cost.value,
5967
0
6068
);
6169

@@ -70,21 +78,31 @@ export const CustomerPayoutServiceCreate = async ({
7078
});
7179

7280
PayoutLogModel.insertMany(
73-
uniqueShippings.map((shipping) => ({
74-
customerId,
75-
referenceId: shipping._id,
76-
referenceType: PayoutLogReferenceType.SHIPPING,
77-
payout: payout._id,
78-
}))
81+
uniqueShippings.map(
82+
(shipping) =>
83+
({
84+
customerId,
85+
referenceId: shipping.shipping._id,
86+
orderId: shipping.id,
87+
orderCreatedAt: shipping.created_at,
88+
referenceType: PayoutLogReferenceType.SHIPPING,
89+
payout: payout._id,
90+
} as PayoutLog)
91+
)
7992
).catch((error) => console.error("Error inserting shipping logs:", error)); //<< needs to send to application inisight
8093

8194
PayoutLogModel.insertMany(
82-
lineItems.map((lineItem) => ({
83-
customerId,
84-
referenceId: lineItem.line_items.id,
85-
referenceType: PayoutLogReferenceType.LINE_ITEM,
86-
payout: payout._id,
87-
}))
95+
orders.map(
96+
(lineItem) =>
97+
({
98+
customerId,
99+
orderId: lineItem.id,
100+
orderCreatedAt: lineItem.created_at,
101+
referenceId: lineItem.line_items.id,
102+
referenceType: PayoutLogReferenceType.LINE_ITEM,
103+
payout: payout._id,
104+
} as PayoutLog)
105+
)
88106
).catch((error) => console.error("Error inserting line item logs:", error)); //<< needs to send to application inisight
89107

90108
return payout.save();
@@ -94,13 +112,17 @@ export const CustomerPayoutServiceGetLineItemsFulfilled = async ({
94112
customerId,
95113
}: CustomerPayoutServiceCreateProps) => {
96114
return OrderModel.aggregate<{
115+
id: number;
116+
created_at: number;
97117
line_items: OrderLineItem;
98118
shipping: Pick<Shipping, "_id" | "cost">;
99119
}>([
100120
...lineItemAggregation({ customerId }),
101121
...shippingAggregation,
102122
{
103123
$project: {
124+
id: "$id",
125+
created_at: "$created_at",
104126
line_items: "$line_items",
105127
shipping: "$shipping",
106128
},

src/functions/payout-log/payout-log.schema.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ export const PayoutLogMongooseSchema = new mongoose.Schema<
2626
required: true,
2727
unique: true,
2828
},
29+
orderId: {
30+
type: Number,
31+
required: true,
32+
index: true,
33+
},
34+
orderCreatedAt: Date,
2935
payout: {
3036
type: mongoose.Schema.Types.ObjectId,
3137
required: true,

src/functions/payout-log/payout-log.types.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ export enum PayoutLogReferenceType {
77
}
88

99
export const PayoutLogZodSchema = z.object({
10-
customerId: NumberOrStringType,
10+
customerId: z.number(),
1111
referenceType: z.nativeEnum(PayoutLogReferenceType),
1212
referenceId: z.union([NumberOrStringType, ObjectIdType]),
13+
orderId: z.number(),
14+
orderCreatedAt: z.number().optional(),
1315
payout: ObjectIdType,
1416
});
1517

0 commit comments

Comments
 (0)