|
1
|
import { |
|
2
|
AuthenticatedMedusaRequest, |
|
3
|
MedusaResponse, |
|
4
|
} from "@medusajs/framework"; |
|
5
|
import { ContainerRegistrationKeys } from "@medusajs/framework/utils"; |
|
6
|
import { |
|
7
|
AdminCreateStoreCreditAccount, |
|
8
|
AdminGetStoreCreditAccountsParams, |
|
9
|
AdminStoreCreditAccount, |
|
10
|
AdminStoreCreditAccountResponse, |
|
11
|
AdminStoreCreditAccountsResponse, |
|
12
|
IStoreCreditModuleService, |
|
13
|
PluginModule, |
|
14
|
} from "../../../types"; |
|
15
|
import { createStoreCreditAccountsWorkflow } from "../../../workflows/store-credit/workflows/create-store-credit-accounts"; |
|
16
|
|
|
17
|
export const GET = async ( |
|
18
|
req: AuthenticatedMedusaRequest<AdminGetStoreCreditAccountsParams>, |
|
19
|
res: MedusaResponse<AdminStoreCreditAccountsResponse> |
|
20
|
) => { |
|
21
|
const query = req.scope.resolve(ContainerRegistrationKeys.QUERY); |
|
22
|
const storeCreditModule = req.scope.resolve<IStoreCreditModuleService>( |
|
23
|
PluginModule.STORE_CREDIT |
|
24
|
); |
|
25
|
|
|
26
|
const { fields, pagination } = req.queryConfig; |
|
27
|
const { data: accounts, metadata } = await query.graph({ |
|
28
|
entity: "store_credit_account", |
|
29
|
fields, |
|
30
|
filters: { ...req.filterableFields }, |
|
31
|
pagination: { |
|
32
|
...pagination, |
|
33
|
skip: pagination.skip!, |
|
34
|
}, |
|
35
|
}); |
|
36
|
|
|
37
|
const finalStoreCreditAccounts: AdminStoreCreditAccount[] = []; |
|
38
|
|
|
39
|
for (const store_credit_account of accounts) { |
|
40
|
const accountStats = await storeCreditModule.retrieveAccountStats({ |
|
41
|
account_id: store_credit_account.id, |
|
42
|
}); |
|
43
|
|
|
44
|
finalStoreCreditAccounts.push({ |
|
45
|
...store_credit_account, |
|
46
|
...accountStats, |
|
47
|
}); |
|
48
|
} |
|
49
|
|
|
50
|
res.json({ |
|
51
|
store_credit_accounts: finalStoreCreditAccounts, |
|
52
|
count: metadata!.count, |
|
53
|
offset: metadata!.skip, |
|
54
|
limit: metadata!.take, |
|
55
|
}); |
|
56
|
}; |
|
57
|
|
|
58
|
export const POST = async ( |
|
59
|
req: AuthenticatedMedusaRequest<AdminCreateStoreCreditAccount>, |
|
60
|
res: MedusaResponse<AdminStoreCreditAccountResponse> |
|
61
|
) => { |
|
62
|
const query = req.scope.resolve(ContainerRegistrationKeys.QUERY); |
|
63
|
|
|
64
|
const { |
|
65
|
result: [storeCreditAccount], |
|
66
|
} = await createStoreCreditAccountsWorkflow.run({ |
|
67
|
input: [{ ...req.validatedBody }], |
|
68
|
container: req.scope, |
|
69
|
}); |
|
70
|
|
|
71
|
const { |
|
72
|
data: [store_credit_account], |
|
73
|
} = await query.graph( |
|
74
|
{ |
|
75
|
entity: "store_credit_accounts", |
|
76
|
fields: req.queryConfig.fields, |
|
77
|
filters: { id: storeCreditAccount.id }, |
|
78
|
}, |
|
79
|
{ throwIfKeyNotFound: true } |
|
80
|
); |
|
81
|
|
|
82
|
res.json({ store_credit_account }); |
|
83
|
}; |