From 9a0c15a6b22d827283a5cdd7b1f01fb8404a40fa Mon Sep 17 00:00:00 2001 From: soy Date: Sat, 6 Jul 2024 14:43:54 +0900 Subject: [PATCH 1/2] refactor: events-from-webhook-http-type --- craft-functions/events-from-webhook/README.md | 6 ++++- craft-functions/events-from-webhook/index.js | 22 +++++++++++++++---- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/craft-functions/events-from-webhook/README.md b/craft-functions/events-from-webhook/README.md index f8fd8a2..43478be 100644 --- a/craft-functions/events-from-webhook/README.md +++ b/craft-functions/events-from-webhook/README.md @@ -13,4 +13,8 @@ https://solution.karte.io/blog/2024/01/webhook-event ## category -外部連携,Craft Functions,CRAFT_ENDPOINT \ No newline at end of file +外部連携,Craft Functions,CRAFT_ENDPOINT + +## functionType + +http \ No newline at end of file diff --git a/craft-functions/events-from-webhook/index.js b/craft-functions/events-from-webhook/index.js index cdeadd9..3b0be71 100644 --- a/craft-functions/events-from-webhook/index.js +++ b/craft-functions/events-from-webhook/index.js @@ -1,4 +1,5 @@ import api from 'api'; + const karteApiClient = api('@dev-karte/v1.0#1jvnhd6llgekil84'); const LOG_LEVEL = '<% LOG_LEVEL %>'; const KARTE_APP_TOKEN_SECRET = '<% KARTE_APP_TOKEN_SECRET %>'; @@ -13,7 +14,9 @@ export default async function (data, { MODULES }) { const secrets = await secret.get({ keys: [KARTE_APP_TOKEN_SECRET] }); const token = secrets[KARTE_APP_TOKEN_SECRET]; karteApiClient.auth(token); - const payload = data.jsonPayload.data.hook_data.body.payload; + + const { req, res } = data; + const payload = req.body.payload; const userId = payload[USER_ID_FIELD]; const values = {}; @@ -22,16 +25,27 @@ export default async function (data, { MODULES }) { values[field] = payload[field]; }); + res.setHeader('Access-Control-Allow-Origin', '*'); + res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS'); + res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization'); + + if (req.method === 'OPTIONS') { + res.status(204).end(); + return; + } + try { await karteApiClient.postV2betaTrackEventWriteandexecaction({ - keys: { visitor_id: VISTOR_ID_PREFIX + '-' + userId }, + keys: { visitor_id: `${VISTOR_ID_PREFIX}-${userId}` }, event: { event_name: EVENT_NAME, values, }, }); - logger.log(EVENT_NAME + 'Event sent successfully.'); + logger.log(`Event sent successfully. event_name: ${EVENT_NAME}`); + res.status(200).send({ message: 'Success' }); } catch (e) { logger.error(e); + res.status(500).send({ error: e.message }); } -} +} \ No newline at end of file From 1f27caa131505691ceacc7dbd59a496bc9d49184 Mon Sep 17 00:00:00 2001 From: soy Date: Mon, 8 Jul 2024 23:33:56 +0900 Subject: [PATCH 2/2] fix: res.send to res.json --- craft-functions/events-from-webhook/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/craft-functions/events-from-webhook/index.js b/craft-functions/events-from-webhook/index.js index 3b0be71..ea0de5c 100644 --- a/craft-functions/events-from-webhook/index.js +++ b/craft-functions/events-from-webhook/index.js @@ -43,9 +43,9 @@ export default async function (data, { MODULES }) { }, }); logger.log(`Event sent successfully. event_name: ${EVENT_NAME}`); - res.status(200).send({ message: 'Success' }); + res.status(200).json({ message: 'Success' }); } catch (e) { logger.error(e); - res.status(500).send({ error: e.message }); + res.status(500).json({ error: e.message }); } } \ No newline at end of file