Repository navigation
Expand file tree
/
Copy pathorderFlow.js
More file actions
288 lines (267 loc) · 9.45 KB
/
Copy pathorderFlow.js
File metadata and controls
288 lines (267 loc) · 9.45 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
const {
ActionRowBuilder,
ButtonBuilder,
ButtonStyle,
ModalBuilder,
SlashCommandBuilder,
TextInputBuilder,
TextInputStyle,
MessageFlags,
} = require('discord.js');
const shopify = require('./shopify');
const shopifyBind = require('./shopifyBind');
const shopifyEmail = require('./shopifyEmail');
const { PersistentVerificationService, normalizeEmail } = require('./verification');
const verification = new PersistentVerificationService();
const ephemeral = { flags: MessageFlags.Ephemeral };
const EARLY_DEFER_MS = 1200;
const orderCommand = new SlashCommandBuilder()
.setName('order')
.setDescription('Check your Shopify order. Verifies the email on the order first.')
.setDefaultMemberPermissions(null)
.addStringOption((option) =>
option.setName('number').setDescription('Optional order number, e.g. #1234').setRequired(false)
)
.toJSON();
const ordersCommand = new SlashCommandBuilder()
.setName('orders')
.setDescription('List recent Shopify orders for your verified email.')
.setDefaultMemberPermissions(null)
.toJSON();
const unlinkCommand = new SlashCommandBuilder()
.setName('unlink')
.setDescription('Forget your verified Shopify order email.')
.setDefaultMemberPermissions(null)
.toJSON();
function isLive() {
return shopify.isConfigured() && shopifyBind.isReady() && shopifyEmail.isReady();
}
function notLiveReply() {
return {
content: 'Order lookup is not live yet. Email help@omi.me with your Order ID.',
...ephemeral,
};
}
function emailModal() {
const modal = new ModalBuilder().setCustomId('link_email').setTitle('Verify your order email');
const email = new TextInputBuilder()
.setCustomId('email')
.setLabel('Email used on the Shopify order')
.setStyle(TextInputStyle.Short)
.setRequired(true)
.setMaxLength(254)
.setPlaceholder('you@example.com');
modal.addComponents(new ActionRowBuilder().addComponents(email));
return modal;
}
function codeModal() {
const modal = new ModalBuilder().setCustomId('verify_code').setTitle('Enter verification code');
const code = new TextInputBuilder()
.setCustomId('code')
.setLabel('6-digit code')
.setStyle(TextInputStyle.Short)
.setRequired(true)
.setMinLength(6)
.setMaxLength(6)
.setPlaceholder('123456');
modal.addComponents(new ActionRowBuilder().addComponents(code));
return modal;
}
async function hold(interaction) {
if (interaction.deferred || interaction.replied) return;
if (typeof interaction.deferReply !== 'function') return;
await interaction.deferReply(ephemeral);
}
async function say(interaction, payload) {
const body = typeof payload === 'string' ? { content: payload } : payload;
if (interaction.deferred || interaction.replied) return interaction.editReply(body);
return interaction.reply({ ...body, ...ephemeral });
}
async function lookupBindingWithEarlyDefer(interaction) {
let deferredResponse;
const timer = setTimeout(() => {
// Observe rejection immediately, even while the database lookup is pending.
deferredResponse = hold(interaction).then(() => null, (error) => error);
}, EARLY_DEFER_MS);
try {
return await shopifyBind.get(interaction.user.id);
} finally {
clearTimeout(timer);
if (deferredResponse) {
const error = await deferredResponse;
if (error) throw error;
}
}
}
function emailVerificationPrompt() {
const button = new ButtonBuilder()
.setCustomId('start_order_verification')
.setLabel('Verify email')
.setStyle(ButtonStyle.Primary);
return {
content: 'Verify the email used on your Shopify order to check your orders.',
components: [new ActionRowBuilder().addComponents(button)],
};
}
async function verifiedOrders(email) {
const found = await shopify.ordersForVerifiedEmail(email);
if (!found.ok) throw new Error(`Shopify lookup failed: ${found.reason}`);
return found.orders;
}
function orderLines(order) {
return shopify.formatUserReply(order);
}
async function replyBoundOrder(interaction, requested, binding) {
await hold(interaction);
const orders = await verifiedOrders(binding.email);
if (!orders.length) {
await say(interaction, 'No recent orders were found for your verified email.');
return true;
}
const want = String(requested || '')
.replace(/^#/, '')
.trim();
const order = want
? orders.find((item) => String(item.name || '').replace(/^#/, '') === want)
: orders[0];
if (!order) {
await say(interaction, `I could not find order #${want} among your recent orders.`);
return true;
}
await say(interaction, orderLines(order));
return true;
}
async function handleOrderInteraction(interaction) {
try {
if (interaction.isChatInputCommand?.()) {
if (!['order', 'orders', 'unlink'].includes(interaction.commandName)) return false;
if (interaction.commandName === 'unlink') {
await hold(interaction);
const removed = await shopifyBind.remove(interaction.user.id);
await say(interaction, {
content: removed ? 'Your Shopify order-email link was removed.' : 'No Shopify email was linked.',
});
return true;
}
if (!isLive()) {
await interaction.reply(notLiveReply());
return true;
}
const binding = await lookupBindingWithEarlyDefer(interaction);
if (!binding) {
if (interaction.deferred || interaction.replied) await say(interaction, emailVerificationPrompt());
else await interaction.showModal(emailModal());
return true;
}
if (interaction.commandName === 'order') {
await replyBoundOrder(interaction, interaction.options?.getString?.('number'), binding);
return true;
}
await hold(interaction);
const orders = await verifiedOrders(binding.email);
if (!orders.length) {
await say(interaction, 'No recent orders were found for your verified email.');
return true;
}
const body = orders
.slice(0, 5)
.map((order) => orderLines(order))
.join('\n\n');
await say(interaction, body);
return true;
}
if (interaction.isModalSubmit?.() && interaction.customId === 'link_email') {
if (!isLive()) {
await interaction.reply(notLiveReply());
return true;
}
const email = normalizeEmail(interaction.fields.getTextInputValue('email'));
if (!email) {
await interaction.reply({ content: 'That does not look like a valid email address.', ...ephemeral });
return true;
}
await hold(interaction);
const reservedAt = await verification.reserveAttempt(interaction.user.id, email);
if (!reservedAt) {
await say(interaction, 'Too many verification requests. Try again later.');
return true;
}
let exists;
try {
exists = await shopify.hasRecentOrderForEmail(email);
} catch (err) {
await verification.releaseAttempt(interaction.user.id, email, reservedAt);
throw err;
}
if (exists) {
const code = await verification.create(interaction.user.id, email);
const sent = await shopifyEmail.sendVerificationCode(email, code);
if (!sent.ok) {
await say(interaction, 'Could not send a verification email. Email help@omi.me with your Order ID.');
return true;
}
}
const button = new ButtonBuilder()
.setCustomId('enter_verification_code')
.setLabel('Enter verification code')
.setStyle(ButtonStyle.Primary);
await say(interaction, {
content:
'If that email matches a recent order, a verification code was sent. The code expires in 10 minutes.',
components: [new ActionRowBuilder().addComponents(button)],
});
return true;
}
if (interaction.isButton?.() && interaction.customId === 'start_order_verification') {
if (!isLive()) await interaction.reply(notLiveReply());
else await interaction.showModal(emailModal());
return true;
}
if (interaction.isButton?.() && interaction.customId === 'enter_verification_code') {
await interaction.showModal(codeModal());
return true;
}
if (interaction.isModalSubmit?.() && interaction.customId === 'verify_code') {
await hold(interaction);
const result = await verification.verify(interaction.user.id, interaction.fields.getTextInputValue('code'));
if (!result.ok) {
await say(interaction, { content: result.reason });
return true;
}
// Verification stores the link and consumes its challenge in one transaction.
let orders;
try {
orders = await verifiedOrders(result.email);
} catch (err) {
console.error('[Bot] order command failed:', err.message);
await say(interaction, 'Verified. Order lookup failed. Try /order again in a moment.');
return true;
}
if (!orders.length) {
await say(interaction, 'Verified. No recent orders are currently accessible.');
return true;
}
await say(interaction, `${orderLines(orders[0])}\n\nYour Discord account is linked. Use /order next time.`);
return true;
}
} catch (err) {
console.error('[Bot] order command failed:', err.message);
if (interaction.isRepliable?.()) {
try {
await say(interaction, 'Order lookup failed. Try again in a moment.');
} catch {
/* ignore */
}
}
return true;
}
return false;
}
module.exports = {
orderCommand,
ordersCommand,
unlinkCommand,
isLive,
handleOrderInteraction,
verification,
};