Skip to content

Commit 38fdca2

Browse files
committed
fix(appmax): Prevent unecessary repeated webhooks (skippable)
1 parent 063eadd commit 38fdca2

1 file changed

Lines changed: 11 additions & 8 deletions

File tree

packages/apps/appmax/lib-mjs/appmax-webhook.mjs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,30 @@ const handleAppmaxWehook = async (req, res) => {
1717
if (!APPMAX_TOKEN) {
1818
return res.sendStatus(403);
1919
}
20-
const { data } = await axios.get(
20+
const response = await axios.get(
2121
`https://admin.appmax.com.br/api/v3/order/${appmaxOrderId}`,
2222
{
2323
params: { 'access-token': APPMAX_TOKEN },
2424
},
2525
{
2626
maxRedirects: 0,
2727
validateStatus(status) {
28-
return status >= 200 && status <= 301;
28+
return (status >= 200 && status <= 301) || status === 403;
2929
},
3030
},
3131
);
32-
const appmaxOrder = data?.data;
32+
if (response.status === 403) {
33+
logger.warn('Appmax order 403 (other store ?)', { appmaxOrderId });
34+
return res.sendStatus(204);
35+
}
36+
const appmaxOrder = response?.data?.data;
3337
if (!appmaxOrder?.status) {
3438
logger.warn('Appmax order status undefined', {
3539
appmaxOrderId,
3640
webhookBody: req.body,
37-
data,
41+
appmaxOrder,
3842
});
39-
return res.sendStatus(412);
43+
return res.sendStatus(204);
4044
}
4145
const {
4246
data: { result: [order] },
@@ -45,12 +49,11 @@ const handleAppmaxWehook = async (req, res) => {
4549
+ '&fields=_id,transactions'
4650
+ '&limit=1');
4751
if (!order) {
48-
const error = `Order not found for ${appmaxOrderId}`;
49-
logger.warn(error, {
52+
logger.warn(`Order not found for ${appmaxOrderId}`, {
5053
body: req.body,
5154
appmaxOrder,
5255
});
53-
return res.sendStatus(409);
56+
return res.sendStatus(204);
5457
}
5558
const transaction = order.transactions?.find(({ intermediator }) => {
5659
return intermediator?.transaction_id === String(appmaxOrderId);

0 commit comments

Comments
 (0)