Skip to content

Commit

Permalink
Merge pull request #372 from claushaas/staging
Browse files Browse the repository at this point in the history
Staging
  • Loading branch information
claushaas authored Oct 23, 2024
2 parents fe505ea + 774928f commit 383cb1a
Show file tree
Hide file tree
Showing 4 changed files with 220 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,5 @@ YEM_API_BASE_URL=
YEM_REMIX_COOKIE_SESSION_SECRET=
SLACK_WEBHOOK_URL=
SLACK_WEBHOOK_URL_FORMATION=
HOTMART_HOTTOK=
HOTMART_HOTTOK=
HUBLA_TOKEN=
52 changes: 52 additions & 0 deletions app/routes/api.hubla.tsx
Original file line number Diff line number Diff line change
@@ -1,2 +1,54 @@
// X-hubla-token
// 8Lui4amoPJRcDXCLN3MSiFZV9HcBiJAzxCcVynu7xcxF2RDBG9LIFopa8yDeodNz

import {json, type LoaderFunctionArgs} from '@remix-run/node';
import {HooksService} from '~/services/hooks.service.server';
import {SlackService} from '~/services/slack.service.server';
import {type THublaEvents} from '~/types/hubla.type';
import {logger} from '~/utils/logger.util';

export const meta = () => [
{name: 'robots', content: 'noindex, nofollow'},
];

export const loader = async ({request}: LoaderFunctionArgs) => {
try {
await new SlackService().sendMessage(await request.json() as Record<string, any>);

return json({
message: 'OK',
}, {
status: 200,
});
} catch (error) {
logger.logError(`Error sending message on hotmart loader: ${(error as Error).message}`);
return json({error: 'An error occurred'});
}
};

export const action = async ({request}: LoaderFunctionArgs) => {
try {
const {headers} = request;
const HUBLA_TOKEN = headers.get('X-hubla-token');

if (HUBLA_TOKEN === process.env.HUBLA_TOKEN) {
const body = await request.json() as THublaEvents;
await new HooksService().handleHublaWebhook(body);

return json({
message: 'OK',
}, {
status: 200,
});
}

return json({
message: 'Unauthorized',
}, {
status: 401,
});
} catch (error) {
logger.logError(`Error sending message on hotmart action: ${(error as Error).message}`);
return json({error: 'An error occurred'});
}
};
22 changes: 22 additions & 0 deletions app/services/hooks.service.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {schoolHotmartPrintedBilletEmailTemplate} from '~/assets/email/school-hot
import {schoolHotmartPrintedPixEmailTemplate} from '~/assets/email/school-hotmart-printed-pix.email.template.server.js';
import {formationHotmartPrintedBilletEmailTemplate} from '~/assets/email/formation-hotmart-printed-billet.email.template.server.js';
import {formationHotmartPrintedPixEmailTemplate} from '~/assets/email/formation-hotmart-printed-pix.email.template.server.js';
import {type THublaEvents, type THublaInvoiceEvents, type THublaSubscriptionEvents} from '~/types/hubla.type.js';

export class HooksService {
private readonly _userService: UserService;
Expand All @@ -41,6 +42,27 @@ export class HooksService {
this._mailService = new MailService();
}

public async handleHublaWebhook(body: THublaEvents): Promise<TServiceReturn<string>> {
const {type} = body;

try {
switch (type) {
default: {
await this._slackService.sendMessage(body);
break;
}
}

return {
status: 'SUCCESSFUL',
data: 'Hubla Webhook received',
};
} catch (error) {
logger.logError(`Error handling hubla webhook: ${(error as Error).message}`);
throw new CustomError('INVALID_DATA', `Error handling hubla webhook: ${(error as Error).message}`);
}
}

public async handleHotmartWebhook(body: TIncommingHotmartWebhook): Promise<TServiceReturn<string>> {
const {event} = body;

Expand Down
144 changes: 144 additions & 0 deletions app/types/hubla.type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
export type THublaInvoiceEvents = {
'type': 'invoice.created' | 'invoice.status_updated' | 'invoice.payment_succeeded' | 'invoice.payment_failed' | 'invoice.expired' | 'invoice.refunded';
'event': {
'product': {
'id': string;
'name': string;
};
'products': [{
'id': string;
'name': string;
'offers': Array<{
'id': string;
'name': string;
'cohorts': Array<{'id': string}>;
}>;
}];
'invoice': {
'id': string;
'subscriptionId': string;
'payerId': string;
'sellerId': string;
'installments': number;
'paymentMethod': string;
'currency': string;
'type': string;
'status': string;
'statusAt': Array<{
'status': string;
'when': string;
}>;
'amount': {
'subtotalCents': number;
'discountCents': number;
'prorataCents': number;
'installmentFeeCents': number;
'totalCents': number;
};
'receivers': Array<{
'id': string;
'name': string;
'email': string;
'phone': string;
'role': string;
'paysForFees': boolean;
'totalCents': number;
}>;
'firstPaymentSession': {
'ip': string;
'utm': {
'source': string;
'medium': string;
'campaign': string;
'content': string;
'term': string;
};
};
'billingAddress': {
'countryCode': string;
'state': string;
'city': string;
'neighborhood': string;
'street': string;
'complement': string;
'number': string;
'postalCode': string;
};
'smartInstallment': {
'sourceInvoiceId': string;
'installment': number;
'installments': number;
};
'saleDate': string;
'dueDate': string;
'createdAt': string;
'modifiedAt': string;
'version': number;
};
'user': {
'id': string;
'firstName': string;
'lastName': string;
'document': string;
'email': string;
'phone': string;
};
};
'version': string;
};

export type THublaSubscriptionEvents = {
'type': 'customer.member_added' | 'customer.member_removed';
'event': {
'product': {
'id': string;
'name': string;
};
'products': Array<{
'id': string;
'name': string;
'offers': Array<{
'id': string;
'name': string;
'cohorts': Array<{'id': string}>;
}>;
}>;
'subscription': {
'id': string;
'sellerId': string;
'payerId': string;
'type': 'recurring' | 'one_time' | 'free';
'status': 'active' | 'inactive';
'billingCycleMonths': 1 | 3 | 6 | 12;
'credits': number;
'paymentMethod': 'credit_card' | 'pix' | 'bank_slip';
'autoRenew': boolean;
'freeTrial': boolean;
'activatedAt': string;
'modifiedAt': string;
'createdAt': string;
'firstPaymentSession': {
'ip': string;
'utm': {
'source': string;
'medium': string;
'campaign': string;
'content': string;
'term': string;
};
};
'version': number;
};
'user': {
'id': string;
'firstName': string;
'lastName': string;
'document': string;
'email': string;
'phone': string;
};
};
'version': string;
};

export type THublaEvents = THublaInvoiceEvents | THublaSubscriptionEvents;

0 comments on commit 383cb1a

Please sign in to comment.