Skip to content

Commit 8e326f1

Browse files
feat: fourthwall update command
1 parent b7d52e6 commit 8e326f1

File tree

5 files changed

+67
-0
lines changed

5 files changed

+67
-0
lines changed

src/discord/commands.ts

+30
Original file line numberDiff line numberDiff line change
@@ -422,4 +422,34 @@ export class DiscordCommands {
422422

423423
return interaction.reply({ content: `Successfully removed ${url}.`, flags: [MessageFlags.SuppressEmbeds] });
424424
}
425+
426+
@Slash({ name: 'fourthwall', description: 'Fourthwall related commmands' })
427+
async handleFourthwall(
428+
@SlashChoice('update')
429+
@SlashOption({
430+
name: 'action',
431+
description: 'update: Updates Fourthwall orders',
432+
required: true,
433+
type: ApplicationCommandOptionType.String,
434+
})
435+
action: 'update',
436+
@SlashOption({
437+
name: 'id',
438+
description: 'Id of a specific order to update',
439+
type: ApplicationCommandOptionType.String,
440+
required: false,
441+
})
442+
id: string | null,
443+
interaction: CommandInteraction,
444+
) {
445+
switch (action) {
446+
case 'update': {
447+
await this.service.updateFourthwallOrders(id);
448+
return interaction.reply({
449+
content: 'Successfully updated Fourthwall orders',
450+
flags: [MessageFlags.Ephemeral],
451+
});
452+
}
453+
}
454+
}
425455
}

src/interfaces/database.interface.ts

+1
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ export interface IDatabaseRepository {
132132
createFourthwallOrder(entity: NewFourthwallOrder): Promise<void>;
133133
updateFourthwallOrder(entity: UpdateFourthwallOrder): Promise<void>;
134134
getTotalFourthwallOrders(options?: ReportOptions): Promise<{ revenue: number; profit: number }>;
135+
streamFourthwallOrders(): AsyncIterableIterator<{ id: string }>;
135136
createRSSFeed(entity: NewRSSFeed): Promise<void>;
136137
getRSSFeeds(channelId?: string): Promise<RSSFeed[]>;
137138
removeRSSFeed(url: string, channelId: string): Promise<void>;

src/repositories/database.repository.ts

+4
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,10 @@ export class DatabaseRepository implements IDatabaseRepository {
196196
return { revenue: Number(revenue) || 0, profit: Number(profit) || 0 };
197197
}
198198

199+
streamFourthwallOrders() {
200+
return this.db.selectFrom('fourthwall_orders').select('id').stream();
201+
}
202+
199203
async createRSSFeed(entity: NewRSSFeed): Promise<void> {
200204
await this.db.insertInto('rss_feeds').values(entity).execute();
201205
}

src/services/discord.service.ts

+30
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { IDatabaseRepository } from 'src/interfaces/database.interface';
99
import { DiscordChannel, IDiscordInterface } from 'src/interfaces/discord.interface';
1010
import { IGithubInterface } from 'src/interfaces/github.interface';
1111
import { IOutlineInterface } from 'src/interfaces/outline.interface';
12+
import { FourthwallRepository } from 'src/repositories/fourthwall.repository';
1213
import { formatCommand, logError, shorten } from 'src/util';
1314

1415
const PREVIEW_BLACKLIST = [Constants.Urls.Immich, Constants.Urls.GitHub, Constants.Urls.MyImmich];
@@ -58,6 +59,7 @@ export class DiscordService {
5859
constructor(
5960
@Inject(IDatabaseRepository) private database: IDatabaseRepository,
6061
@Inject(IDiscordInterface) private discord: IDiscordInterface,
62+
@Inject(FourthwallRepository) private fourthwall: FourthwallRepository,
6163
@Inject(IGithubInterface) private github: IGithubInterface,
6264
@Inject(IOutlineInterface) private outline: IOutlineInterface,
6365
) {}
@@ -464,4 +466,32 @@ export class DiscordService {
464466
}
465467
}
466468
}
469+
470+
async updateFourthwallOrders(id?: string | null) {
471+
const {
472+
fourthwall: { user, password },
473+
} = getConfig();
474+
475+
if (id) {
476+
await this.updateOrder({ id, user, password });
477+
}
478+
479+
for await (const { id } of this.database.streamFourthwallOrders()) {
480+
await this.updateOrder({ id, user, password });
481+
}
482+
}
483+
484+
private async updateOrder({ id, user, password }: { id: string; user: string; password: string }) {
485+
const order = await this.fourthwall.getOrder({ id, user, password });
486+
487+
await this.database.updateFourthwallOrder({
488+
id,
489+
discount: order.discount,
490+
status: order.status,
491+
total: order.totalPrice.value,
492+
profit: order.profit.value,
493+
shipping: order.currentAmounts.shipping.value,
494+
tax: order.currentAmounts.tax.value,
495+
});
496+
}
467497
}

src/services/webhook.service.ts

+2
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,8 @@ export class WebhookService {
207207
}
208208
const dtoOrder = dto.type === 'ORDER_PLACED' ? dto.data : dto.data.order;
209209

210+
await new Promise((resolve) => setTimeout(resolve, 10_000));
211+
210212
let order = await this.fourthwall.getOrder({
211213
id: dtoOrder.id,
212214
user: fourthwall.user,

0 commit comments

Comments
 (0)