1
1
import { OrderModel } from "~/functions/order/order.models" ;
2
2
import { OrderLineItem } from "~/functions/order/order.types" ;
3
3
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" ;
5
9
import { Shipping } from "~/functions/shipping/shipping.types" ;
6
10
import { NotFoundError } from "~/library/handler" ;
7
11
import { CustomerPayoutAccountServiceGet } from "../payout-account/get" ;
@@ -14,11 +18,11 @@ export type CustomerPayoutServiceCreateProps = {
14
18
export const CustomerPayoutServiceCreate = async ( {
15
19
customerId,
16
20
} : CustomerPayoutServiceCreateProps ) => {
17
- const lineItems = await CustomerPayoutServiceGetLineItemsFulfilled ( {
21
+ const orders = await CustomerPayoutServiceGetLineItemsFulfilled ( {
18
22
customerId,
19
23
} ) ;
20
24
21
- if ( lineItems . length === 0 ) {
25
+ if ( orders . length === 0 ) {
22
26
throw new NotFoundError ( [
23
27
{
24
28
code : "custom" ,
@@ -39,23 +43,27 @@ export const CustomerPayoutServiceCreate = async ({
39
43
] ) ;
40
44
}
41
45
42
- const totalLineItems = lineItems . reduce (
46
+ const totalLineItems = orders . reduce (
43
47
( accumulator , { line_items } ) => accumulator + parseFloat ( line_items . price ) ,
44
48
0
45
49
) ;
46
50
47
- const shippings = lineItems
51
+ const shippings = orders
48
52
. filter ( ( lineItem ) => lineItem . shipping )
49
- . map ( ( { shipping } ) => shipping ) ;
53
+ . map ( ( { id, created_at, shipping } ) => ( {
54
+ id,
55
+ created_at,
56
+ shipping,
57
+ } ) ) ;
50
58
51
59
let uniqueShippings = Array . from (
52
60
new Map (
53
- shippings . map ( ( shipping ) => [ shipping . _id . toString ( ) , shipping ] )
61
+ shippings . map ( ( shipping ) => [ shipping . shipping . _id . toString ( ) , shipping ] )
54
62
) . values ( )
55
63
) ;
56
64
57
65
const totalShippingAmount = uniqueShippings . reduce (
58
- ( accumulator , { cost } ) => accumulator + cost . value ,
66
+ ( accumulator , { shipping } ) => accumulator + shipping . cost . value ,
59
67
0
60
68
) ;
61
69
@@ -70,21 +78,31 @@ export const CustomerPayoutServiceCreate = async ({
70
78
} ) ;
71
79
72
80
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
+ )
79
92
) . catch ( ( error ) => console . error ( "Error inserting shipping logs:" , error ) ) ; //<< needs to send to application inisight
80
93
81
94
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
+ )
88
106
) . catch ( ( error ) => console . error ( "Error inserting line item logs:" , error ) ) ; //<< needs to send to application inisight
89
107
90
108
return payout . save ( ) ;
@@ -94,13 +112,17 @@ export const CustomerPayoutServiceGetLineItemsFulfilled = async ({
94
112
customerId,
95
113
} : CustomerPayoutServiceCreateProps ) => {
96
114
return OrderModel . aggregate < {
115
+ id : number ;
116
+ created_at : number ;
97
117
line_items : OrderLineItem ;
98
118
shipping : Pick < Shipping , "_id" | "cost" > ;
99
119
} > ( [
100
120
...lineItemAggregation ( { customerId } ) ,
101
121
...shippingAggregation ,
102
122
{
103
123
$project : {
124
+ id : "$id" ,
125
+ created_at : "$created_at" ,
104
126
line_items : "$line_items" ,
105
127
shipping : "$shipping" ,
106
128
} ,
0 commit comments