1
- import { ActionOptions , ChargeShopGlobalActionContext } from "gadget-server" ;
1
+ import { ActionOptions } from "gadget-server" ;
2
2
import { getCappedAmount } from "../../utilities" ;
3
-
4
- /**
5
- * @param { ChargeShopGlobalActionContext } context
6
- */
7
- export async function run ( { params, logger, api, connections } ) {
8
- const { shop, order } = params ;
3
+ import type { SubscriptionLineItems } from "../models/shopifyShop/actions/subscribe" ;
4
+
5
+ export const run : ActionRun = async ( { params, logger, api, connections } ) => {
6
+ const {
7
+ shop,
8
+ order,
9
+ } : {
10
+ shop ?: {
11
+ id ?: string ;
12
+ overage ?: number ;
13
+ activeSubscriptionId ?: string ;
14
+ usagePlanId ?: string ;
15
+ plan ?: {
16
+ price ?: number ;
17
+ currency ?: string ;
18
+ } ;
19
+ } ;
20
+ order ?: {
21
+ email ?: string ;
22
+ } ;
23
+ } = params ;
24
+
25
+ if ( ! shop || ! shop . id || ! shop . activeSubscriptionId )
26
+ throw new Error ( "Missing required shop data" ) ;
9
27
10
28
// Creating an instance of the Shopify Admin API
11
- const shopify = await connections . shopify . forShopId ( shop . id ) ;
29
+ const shopify = await connections . shopify . forShopId ( shop ? .id ) ;
12
30
13
31
// Returning early if the shop is uninstalled and no Shopify instance is created
14
32
if ( ! shopify )
15
33
return logger . warn ( {
16
34
message : "BILLING - Shop uninstalled" ,
17
- shopId : shop . id ,
35
+ shopId : shop ? .id ,
18
36
} ) ;
19
37
20
38
// Fetching the amount used in the period
21
- const { amountUsedInPeriod } = await api . shopifyShop . findOne ( shop . id , {
22
- amountUsedInPeriod : true ,
39
+ const { amountUsedInPeriod } = await api . shopifyShop . findOne ( shop ?. id , {
40
+ select : {
41
+ amountUsedInPeriod : true ,
42
+ } ,
23
43
} ) ;
24
44
25
45
let remainder = 0 ;
@@ -37,31 +57,33 @@ export async function run({ params, logger, api, connections }) {
37
57
if ( ! subscription )
38
58
return logger . warn ( {
39
59
message : "BILLING - No subscription found for the shop" ,
40
- shopId : shop . id ,
60
+ shopId : shop ? .id ,
41
61
} ) ;
42
62
43
63
// Pulling out the capped amount from the active subscription
44
- const cappedAmount = getCappedAmount ( subscription ) ;
64
+ const cappedAmount = getCappedAmount (
65
+ subscription . lineItems as SubscriptionLineItems
66
+ ) ;
45
67
46
68
if ( ! cappedAmount )
47
69
return logger . warn ( {
48
70
message : "BILLING - No capped amount found for the shop" ,
49
- shopId : shop . id ,
71
+ shopId : shop ? .id ,
50
72
} ) ;
51
73
52
74
// Initially setting the price to the plan price and adding the overage amount if they exist
53
- let price = shop . plan ?. price + ( shop . overage || 0 ) || 0 ;
75
+ let price = shop ? .plan ?. price ?? 0 + ( shop ? .overage ?? 0 ) ;
54
76
55
77
// Returning early if the amount used in the period is greater than or equal to the capped amount
56
- if ( amountUsedInPeriod >= cappedAmount ) {
78
+ if ( amountUsedInPeriod && ( amountUsedInPeriod as number ) >= cappedAmount ) {
57
79
// Modifying the overage amount of the current shop
58
- return await api . internal . shopifyShop . update ( shop . id , {
80
+ return await api . internal . shopifyShop . update ( shop ? .id , {
59
81
overage : price ,
60
82
} ) ;
61
83
}
62
84
63
85
// Calculating the available amount
64
- const availableAmount = cappedAmount - amountUsedInPeriod ;
86
+ const availableAmount = cappedAmount - ( ( amountUsedInPeriod as number ) ?? 0 ) ;
65
87
66
88
// Setting a remainder if the price is greater than the available amount
67
89
if ( price >= availableAmount ) {
@@ -77,22 +99,21 @@ export async function run({ params, logger, api, connections }) {
77
99
id
78
100
}
79
101
userErrors {
80
- field
81
102
message
82
103
}
83
104
}
84
105
}` ,
85
106
{
86
- description : shop . overage
87
- ? `Charge of ${ price } ${ shop . currency } ${
88
- order . email ? `for order placed by ${ order . email } ` : ""
107
+ description : shop ? .overage
108
+ ? `Charge of ${ price } ${ shop ?. plan ? .currency } ${
109
+ order ? .email ? `for order placed by ${ order ? .email } ` : ""
89
110
} , with overages from the previous billing period`
90
- : `Charge of ${ price } ${ shop . currency } for order placed by ${ order . email } ` ,
111
+ : `Charge of ${ price } ${ shop ?. plan ?. currency } for order placed by ${ order ? .email } ` ,
91
112
price : {
92
113
amount : price ,
93
- currencyCode : shop . currency ,
114
+ currencyCode : shop ?. plan ? .currency ,
94
115
} ,
95
- subscriptionLineItemId : shop . usagePlanId ,
116
+ subscriptionLineItemId : shop ? .usagePlanId ,
96
117
}
97
118
) ;
98
119
@@ -104,21 +125,20 @@ export async function run({ params, logger, api, connections }) {
104
125
await api . internal . usageRecord . create ( {
105
126
id : result . appUsageRecordCreate . appUsageRecord . id . split ( "/" ) [ 4 ] ,
106
127
price,
107
- currency : shop . currency ,
128
+ currency : shop ?. plan ? .currency ,
108
129
shop : {
109
- _link : shop . id ,
130
+ _link : shop ? .id ,
110
131
} ,
111
132
} ) ;
112
133
113
134
// Updating the overage amount if there is a remainder
114
135
if ( remainder )
115
- await api . internal . shopifyShop . update ( shop . id , {
136
+ await api . internal . shopifyShop . update ( shop ? .id , {
116
137
overage : remainder ,
117
138
} ) ;
118
- }
139
+ } ;
119
140
120
- /** @type { ActionOptions } */
121
- export const options = { } ;
141
+ export const options : ActionOptions = { } ;
122
142
123
143
export const params = {
124
144
shop : {
@@ -133,9 +153,6 @@ export const params = {
133
153
usagePlanId : {
134
154
type : "string" ,
135
155
} ,
136
- currency : {
137
- type : "string" ,
138
- } ,
139
156
overage : {
140
157
type : "number" ,
141
158
} ,
@@ -145,6 +162,9 @@ export const params = {
145
162
price : {
146
163
type : "number" ,
147
164
} ,
165
+ currency : {
166
+ type : "string" ,
167
+ } ,
148
168
} ,
149
169
} ,
150
170
} ,
0 commit comments