From 4e906a17c596169a25a1bee19e66caba19392195 Mon Sep 17 00:00:00 2001 From: soy Date: Sun, 7 Jul 2024 10:44:16 +0900 Subject: [PATCH 1/2] refactor: pollen-api --- craft-functions/pollen-info/README.md | 6 +++++- craft-functions/pollen-info/index.js | 21 +++++++++++++++++++-- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/craft-functions/pollen-info/README.md b/craft-functions/pollen-info/README.md index 025ea19..2df4101 100644 --- a/craft-functions/pollen-info/README.md +++ b/craft-functions/pollen-info/README.md @@ -13,4 +13,8 @@ https://solution.karte.io/blog/2024/03/get-pollen-info-with-google-pollen-api ## category -Google Maps Platform,Pollen API,Craft Functions,KARTEイベント,接客サービス,CRAFT_ENDPOINT \ No newline at end of file +Google Maps Platform,Pollen API,Craft Functions,KARTEイベント,接客サービス,CRAFT_ENDPOINT + +## functionType + +http \ No newline at end of file diff --git a/craft-functions/pollen-info/index.js b/craft-functions/pollen-info/index.js index 747a231..39524c1 100644 --- a/craft-functions/pollen-info/index.js +++ b/craft-functions/pollen-info/index.js @@ -27,17 +27,30 @@ async function fetchPollenInfo(latitude, longitude, logger) { } export default async function (data, { MODULES }) { + const { req, res } = data; const { initLogger, secret } = MODULES; const logger = initLogger({ logLevel: LOG_LEVEL }); - const body = data.jsonPayload.data.hook_data.body; + + 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; + } + + const body = req.body; if (!body) { logger.error('bodyが存在しません'); + res.status(400).send({ message: 'bodyが存在しません' }); return; } const visitorId = body.visitor_id; if (!visitorId) { logger.error('visitor_idが存在しません'); + res.status(400).send({ message: 'visitor_idが存在しません' }); return; } @@ -45,6 +58,7 @@ export default async function (data, { MODULES }) { const longitude = body.longitude; if (!latitude || !longitude) { logger.error('latitudeかlongitudeのいずれかが存在しません'); + res.status(400).send({ message: 'latitudeかlongitudeのいずれかが存在しません' }); return; } @@ -54,6 +68,7 @@ export default async function (data, { MODULES }) { const pollen = await fetchPollenInfo(latitude, longitude, logger); if (!pollen) { + res.status(500).send({ message: 'Pollen情報の取得に失敗しました' }); return; } const values = {}; @@ -68,7 +83,9 @@ export default async function (data, { MODULES }) { }, }); logger.log(`Event sent successfully. event_name: ${EVENT_NAME}`); + res.status(200).send({ message: 'Event sent successfully' }); } catch (e) { logger.error(`send event failed. event_name: ${EVENT_NAME}, error: ${e}`); + res.status(500).send({ message: `send event failed. event_name: ${EVENT_NAME}, error: ${e}` }); } -} +} \ No newline at end of file From 5d3490b55775bdad55a644fb82e25574b2f8aa28 Mon Sep 17 00:00:00 2001 From: soy Date: Tue, 9 Jul 2024 06:46:09 +0900 Subject: [PATCH 2/2] refactor/pollen-api --- craft-functions/pollen-info/index.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/craft-functions/pollen-info/index.js b/craft-functions/pollen-info/index.js index 39524c1..9794086 100644 --- a/craft-functions/pollen-info/index.js +++ b/craft-functions/pollen-info/index.js @@ -43,14 +43,14 @@ export default async function (data, { MODULES }) { const body = req.body; if (!body) { logger.error('bodyが存在しません'); - res.status(400).send({ message: 'bodyが存在しません' }); + res.status(400).json({ message: 'bodyが存在しません' }); return; } const visitorId = body.visitor_id; if (!visitorId) { logger.error('visitor_idが存在しません'); - res.status(400).send({ message: 'visitor_idが存在しません' }); + res.status(400).json({ message: 'visitor_idが存在しません' }); return; } @@ -58,7 +58,7 @@ export default async function (data, { MODULES }) { const longitude = body.longitude; if (!latitude || !longitude) { logger.error('latitudeかlongitudeのいずれかが存在しません'); - res.status(400).send({ message: 'latitudeかlongitudeのいずれかが存在しません' }); + res.status(400).json({ message: 'latitudeかlongitudeのいずれかが存在しません' }); return; } @@ -68,7 +68,7 @@ export default async function (data, { MODULES }) { const pollen = await fetchPollenInfo(latitude, longitude, logger); if (!pollen) { - res.status(500).send({ message: 'Pollen情報の取得に失敗しました' }); + res.status(500).json({ message: 'Pollen情報の取得に失敗しました' }); return; } const values = {}; @@ -83,9 +83,9 @@ export default async function (data, { MODULES }) { }, }); logger.log(`Event sent successfully. event_name: ${EVENT_NAME}`); - res.status(200).send({ message: 'Event sent successfully' }); + res.status(200).json({ message: 'Event sent successfully' }); } catch (e) { logger.error(`send event failed. event_name: ${EVENT_NAME}, error: ${e}`); - res.status(500).send({ message: `send event failed. event_name: ${EVENT_NAME}, error: ${e}` }); + res.status(500).json({ message: `send event failed. event_name: ${EVENT_NAME}, error: ${e}` }); } } \ No newline at end of file