@@ -21,6 +21,8 @@ import LoginAttemptTemplate from "../../../../Email/Templates/Customer/LoginAtte
2121import ResetPasswordTemplate from "../../../../Email/Templates/Customer/ResetPassword.template" ;
2222import OrderCancelTemplate from "../../../../Email/Templates/Customer/OrderCancel.template" ;
2323import Header from "../../../../Email/Templates/General/Header" ;
24+ import MongoFind from "../../../../Lib/MongoFind" ;
25+ import createPDFInvoice from "../../../../Lib/Invoices/CreatePDFInvoice" ;
2426
2527export = class CustomerRouter
2628{
@@ -53,10 +55,10 @@ export = class CustomerRouter
5355 if ( ! customer )
5456 return APIError ( `Unable to find customer` ) ( res ) ;
5557
56- const invoices = await InvoiceModel . find ( {
58+ const invoices = await MongoFind ( InvoiceModel , req . query , {
5759 $or : [
58- { customer_uid : customer . uid } ,
59- { customer_uid : customer . id }
60+ { customer_uid : customer . uid } ,
61+ { customer_uid : customer . id }
6062 ]
6163 } ) ;
6264
@@ -78,7 +80,7 @@ export = class CustomerRouter
7880 if ( ! customer )
7981 return APIError ( `Unable to find customer` ) ( res ) ;
8082
81- const invoice = await InvoiceModel . findOne ( {
83+ const [ invoice ] = await MongoFind ( InvoiceModel , req . query , {
8284 // lol almost forgot to add customer_uid kek
8385 $or : [
8486 {
@@ -97,6 +99,46 @@ export = class CustomerRouter
9799 return APISuccess ( invoice ) ( res ) ;
98100 } ) ;
99101
102+ this . router . get ( "/my/invoices/:id/preview" , EnsureAuth ( ) , async ( req , res ) =>
103+ {
104+ const invoiceId = req . params . id ;
105+
106+ if ( ! invoiceId )
107+ return APIError ( `Invalid invoice id` ) ( res ) ;
108+
109+ const customer = await CustomerModel . findOne ( {
110+ // @ts -ignore
111+ id : req . customer . id
112+ } ) ;
113+
114+ if ( ! customer )
115+ return APIError ( `Unable to find customer` ) ( res ) ;
116+
117+ const [ invoice ] = await MongoFind ( InvoiceModel , req . query , {
118+ // lol almost forgot to add customer_uid kek
119+ $or : [
120+ {
121+ customer_uid : customer . uid ,
122+ } ,
123+ {
124+ customer_uid : customer . id ,
125+ } ,
126+ ] ,
127+ id : invoiceId ,
128+ } ) ;
129+
130+ if ( ! invoice )
131+ return APIError ( `Unable to find invoice` ) ( res ) ;
132+
133+ const result = await createPDFInvoice ( invoice ) ;
134+
135+ res . writeHead ( 200 , {
136+ 'Content-Type' : "application/pdf" ,
137+ } ) ;
138+
139+ return res . end ( result , "base64" ) ;
140+ } ) ;
141+
100142 this . router . get ( "/my/orders" , EnsureAuth ( ) , async ( req , res ) =>
101143 {
102144 const customer = await CustomerModel . findOne ( {
@@ -107,10 +149,10 @@ export = class CustomerRouter
107149 if ( ! customer )
108150 return APIError ( `Unable to find customer` ) ( res ) ;
109151
110- const orders = await OrderModel . find ( {
152+ const orders = await MongoFind ( OrderModel , req . query , {
111153 $or : [
112- { customer_uid : customer . uid } ,
113- { customer_uid : customer . id }
154+ { customer_uid : customer . uid } ,
155+ { customer_uid : customer . id }
114156 ]
115157 } ) ;
116158
@@ -132,7 +174,7 @@ export = class CustomerRouter
132174 if ( ! customer )
133175 return APIError ( `Unable to find customer` ) ( res ) ;
134176
135- const order = await OrderModel . findOne ( {
177+ const [ order ] = await MongoFind ( OrderModel , req . query , {
136178 $or : [
137179 {
138180 customer_uid : customer . uid ,
@@ -215,7 +257,7 @@ export = class CustomerRouter
215257 if ( ! customer )
216258 return APIError ( `Unable to find customer` ) ( res ) ;
217259
218- const transactions = await TransactionsModel . find ( {
260+ const transactions = await MongoFind ( TransactionsModel , req . query , {
219261 $or : [
220262 { customer_uid : customer . uid } ,
221263 { customer_uid : customer . id }
@@ -240,7 +282,7 @@ export = class CustomerRouter
240282 if ( ! customer )
241283 return APIError ( `Unable to find customer` ) ( res ) ;
242284
243- const transactions = await TransactionsModel . find ( {
285+ const [ transactions ] = await MongoFind ( TransactionsModel , req . query , {
244286 $or : [
245287 {
246288 customer_uid : customer . uid ,
0 commit comments