/
sanity-product-deleted.ts
153 lines (137 loc) · 3.85 KB
/
sanity-product-deleted.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import {inngest} from 'inngest/inngest.server'
import {prisma} from '@skillrecordings/database'
import {SANITY_WEBHOOK_EVENT} from '../sanity-inngest-events'
import {paymentOptions} from 'pages/api/skill/[...skillRecordings]'
const stripe = paymentOptions.providers.stripe?.paymentClient
export const sanityProductDeleted = inngest.createFunction(
{id: `product-delete`, name: 'Deactivate Product in Database'},
{
event: SANITY_WEBHOOK_EVENT,
if: 'event.data.event == "product.delete"',
},
async ({event, step}) => {
if (!stripe) {
throw new Error('Payment provider (Stripe) is missing')
}
const {productId} = event.data
if (!productId) {
throw new Error(`Product id not found`)
}
const product = await step.run('get product in database', async () => {
return await prisma.product.findUnique({
where: {
id: productId,
},
include: {
prices: true,
},
})
})
if (!product) {
throw new Error(`Product not found [${productId}]`)
}
await step.run('deactivate product', async () => {
return await prisma.product.update({
where: {
id: productId,
},
data: {
status: 0,
name: `(DEACTIVATED) ${product.name}`,
},
})
})
await step.run('deactivate prices', async () => {
return await Promise.all(
product.prices.map(async (price) => {
return await prisma.price.update({
where: {
id: price.id,
},
data: {
status: 0,
nickname: `(DEACTIVATED) ${price.nickname}`,
},
})
}),
)
})
const merchantProduct = await step.run('get merchant product', async () => {
return await prisma.merchantProduct.findFirst({
where: {
productId,
},
include: {
merchantPrices: true,
},
})
})
if (!merchantProduct) {
throw new Error(
`Merchant Product not found for product id [${productId}]`,
)
}
await step.run('deactivate merchant product', async () => {
return await prisma.merchantProduct.update({
where: {
id: merchantProduct.id,
},
data: {
status: 0,
},
})
})
await step.run('deactivate merchant prices', async () => {
return await Promise.all(
merchantProduct.merchantPrices.map(async (merchantPrice) => {
return await prisma.merchantPrice.update({
where: {
id: merchantPrice.id,
},
data: {
status: 0,
},
})
}),
)
})
const stripeProduct = await step.run('get stripe product', async () => {
if (!merchantProduct?.identifier) {
throw new Error('Merchant Product not found')
}
return await stripe.products.retrieve(merchantProduct.identifier)
})
await step.run('deactivate stripe product', async () => {
return await stripe.products.update(stripeProduct.id, {
active: false,
})
})
await step.run('delete product upgrades', async () => {
return await prisma.upgradableProducts.deleteMany({
where: {
OR: [
{
upgradableFromId: product.id,
},
{
upgradableToId: product.id,
},
],
},
})
})
await step.run('deactivate all stripe prices', async () => {
return await Promise.all(
merchantProduct.merchantPrices.map(async (merchantPrice) => {
if (merchantPrice.identifier) {
return stripe.prices.update(merchantPrice.identifier, {
active: false,
})
} else {
return Promise.resolve()
}
}),
)
})
},
)