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: eventwrite-from-googleform-http-type #153

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/event_writte_from_google_form/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@ https://solution.karte.io/blog/2023/12/eventwrite-from-googleform

## category

イベント発火,Craft Functions,Google Form,CRAFT_ENDPOINT
イベント発火,Craft Functions,Google Form,CRAFT_ENDPOINT

## functionType

http
33 changes: 23 additions & 10 deletions craft-functions/event_writte_from_google_form/index.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,31 @@
import api from "api";
import api from 'api';

const LOG_LEVEL = "<% LOG_LEVEL %>"; // ログのレベルを定義
const SECRET_NAME = "<% SECRET_NAME %>"; // 作成したシークレットの名前を定義
const karteApiClient = api("@dev-karte/v1.0#1jvnhd6llgekil84");
const LOG_LEVEL = '<% LOG_LEVEL %>'; // ログのレベルを定義
const KARTE_APP_TOKEN_SECRET = '<% KARTE_APP_TOKEN_SECRET %>'; // 作成したシークレットの名前を定義
const karteApiClient = api('@dev-karte/v1.0#1jvnhd6llgekil84');

export default async function (data, { MODULES }) {
const { initLogger, secret } = MODULES;
const logger = initLogger({ logLevel: LOG_LEVEL });
const secrets = await secret.get({ keys: [SECRET_NAME] });
const token = secrets[SECRET_NAME];
const secrets = await secret.get({ keys: [KARTE_APP_TOKEN_SECRET] });
const token = secrets[KARTE_APP_TOKEN_SECRET];
karteApiClient.auth(token);

const eventName = data.jsonPayload.data.hook_data.body.event_name; // フォームに入力されたイベント名を取得
const userId = data.jsonPayload.data.hook_data.body.user_id; // イベントを送信するユーザーのIDを指定する
const valuesString = data.jsonPayload.data.hook_data.body.values; // イベントの中で送信したいvaluesを定義
const { req, res } = data;
const eventName = req.body.event_name; // フォームに入力されたイベント名を取得
const userId = req.body.user_id; // イベントを送信するユーザーのIDを指定する
const valuesString = req.body.values; // イベントの中で送信したいvaluesを定義
const values = JSON.parse(valuesString); // イベントの文字列をObjectにParse

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: { user_id: userId },
Expand All @@ -24,7 +35,9 @@ export default async function (data, { MODULES }) {
},
});
logger.log(`Event sent successfully.`);
res.status(200).json({ message: 'Success' });
} catch (e) {
logger.error(e);
res.status(500).json({ error: e.message });
}
}
}