Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: pollen-api #161

Merged
merged 2 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion craft-functions/pollen-info/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Google Maps Platform,Pollen API,Craft Functions,KARTEイベント,接客サービス,CRAFT_ENDPOINT

## functionType

http
21 changes: 19 additions & 2 deletions craft-functions/pollen-info/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,38 @@ 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).json({ message: 'bodyが存在しません' });
return;
}

const visitorId = body.visitor_id;
if (!visitorId) {
logger.error('visitor_idが存在しません');
res.status(400).json({ message: 'visitor_idが存在しません' });
return;
}

const latitude = body.latitude;
const longitude = body.longitude;
if (!latitude || !longitude) {
logger.error('latitudeかlongitudeのいずれかが存在しません');
res.status(400).json({ message: 'latitudeかlongitudeのいずれかが存在しません' });
return;
}

Expand All @@ -54,6 +68,7 @@ export default async function (data, { MODULES }) {

const pollen = await fetchPollenInfo(latitude, longitude, logger);
if (!pollen) {
res.status(500).json({ message: 'Pollen情報の取得に失敗しました' });
return;
}
const values = {};
Expand All @@ -68,7 +83,9 @@ export default async function (data, { MODULES }) {
},
});
logger.log(`Event sent successfully. event_name: ${EVENT_NAME}`);
res.status(200).json({ message: 'Event sent successfully' });
} catch (e) {
logger.error(`send event failed. event_name: ${EVENT_NAME}, error: ${e}`);
res.status(500).json({ message: `send event failed. event_name: ${EVENT_NAME}, error: ${e}` });
}
}
}