Skip to content

Commit

Permalink
fix: fw webhook types
Browse files Browse the repository at this point in the history
  • Loading branch information
danieldietzler committed Feb 26, 2025
1 parent b34066f commit 5295ed5
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 30 deletions.
4 changes: 2 additions & 2 deletions src/interfaces/fourthwall.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ export interface FourthwallOrderCreateWebhook {
type: 'ORDER_PLACED';
apiVersion: string;
createdAt: string;
data: FourthwallOrderData;
data: { order: FourthwallOrderData };
}

interface FourthwallOrderData {
Expand Down Expand Up @@ -251,7 +251,7 @@ export interface FourthwallOrderUpdateWebhook {
type: 'ORDER_UPDATED';
apiVersion: string;
createdAt: string;
data: FourthwallOrderData & { update: { type: string } };
data: { order: FourthwallOrderData; update: { type: string } };
}

export interface IFourthwallRepository {
Expand Down
60 changes: 32 additions & 28 deletions src/services/webhook.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,53 +207,55 @@ export class WebhookService {
}

let order = await this.fourthwall.getOrder({
id: dto.data.id,
id: dto.data.order.id,
user: fourthwall.user,
password: fourthwall.password,
});

if (dto.testMode) {
order = {
profit: {
value: dto.data.amounts.subtotal.value - Math.random() * dto.data.amounts.subtotal.value,
value: dto.data.order.amounts.subtotal.value - Math.random() * dto.data.order.amounts.subtotal.value,
currency: 'USD',
},
} as any;
}

console.log(dto);

switch (dto.type) {
case 'ORDER_PLACED': {
await this.database.createFourthwallOrder({
id: dto.data.id,
discount: dto.data.amounts.discount.value,
tax: dto.data.amounts.tax.value,
shipping: dto.data.amounts.shipping.value,
subtotal: dto.data.amounts.subtotal.value,
total: dto.data.amounts.total.value,
revenue: dto.data.amounts.subtotal.value,
id: dto.data.order.id,
discount: dto.data.order.amounts.discount.value,
tax: dto.data.order.amounts.tax.value,
shipping: dto.data.order.amounts.shipping.value,
subtotal: dto.data.order.amounts.subtotal.value,
total: dto.data.order.amounts.total.value,
revenue: dto.data.order.amounts.subtotal.value,
profit: order.profit.value,
username: dto.data.username,
message: dto.data.message,
status: dto.data.status,
createdAt: new Date(dto.data.createdAt),
username: dto.data.order.username,
message: dto.data.order.message,
status: dto.data.order.status,
createdAt: new Date(dto.data.order.createdAt),
testMode: dto.testMode,
});
break;
}
case 'ORDER_UPDATED': {
await this.database.updateFourthwallOrder({
id: dto.data.id,
discount: dto.data.amounts.discount.value,
tax: dto.data.amounts.tax.value,
shipping: dto.data.amounts.shipping.value,
subtotal: dto.data.amounts.subtotal.value,
total: dto.data.amounts.total.value,
revenue: dto.data.amounts.subtotal.value,
id: dto.data.order.id,
discount: dto.data.order.amounts.discount.value,
tax: dto.data.order.amounts.tax.value,
shipping: dto.data.order.amounts.shipping.value,
subtotal: dto.data.order.amounts.subtotal.value,
total: dto.data.order.amounts.total.value,
revenue: dto.data.order.amounts.subtotal.value,
profit: order.profit.value,
username: dto.data.username,
message: dto.data.message,
status: dto.data.status,
createdAt: new Date(dto.data.createdAt),
username: dto.data.order.username,
message: dto.data.order.message,
status: dto.data.order.status,
createdAt: new Date(dto.data.order.createdAt),
});
break;
}
Expand All @@ -269,13 +271,15 @@ export class WebhookService {
.setTitle(
`${dto.testMode ? 'TEST ORDER - ' : ''}Immich merch ${dto.type === 'ORDER_PLACED' ? 'purchased' : 'order updated'}`,
)
.setURL(`https://immich-shop.fourthwall.com/admin/dashboard/contributions/orders/${dto.data.id}`)
.setURL(`https://immich-shop.fourthwall.com/admin/dashboard/contributions/orders/${dto.data.order.id}`)
.setAuthor({ name: 'Fourthwall', url: 'https://fourthwall.com' })
.setDescription(
`Price: ${dto.data.amounts.subtotal.value.toLocaleString()} USD; Profit: ${order.profit.value.toLocaleString()} USD`,
`Price: ${dto.data.order.amounts.subtotal.value.toLocaleString()} USD; Profit: ${order.profit.value.toLocaleString()} USD`,
)
.setColor(
dto.testMode ? Colors.Yellow : dto.data.order.status === 'CANCELLED' ? Colors.Red : Colors.DarkGreen,
)
.setColor(dto.testMode ? Colors.Yellow : dto.data.status === 'CANCELLED' ? Colors.Red : Colors.DarkGreen)
.setFields(makeOrderFields({ revenue, profit, message: dto.data.message })),
.setFields(makeOrderFields({ revenue, profit, message: dto.data.order.message })),
],
flags: [MessageFlags.SuppressNotifications],
},
Expand Down

0 comments on commit 5295ed5

Please sign in to comment.