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: actiontable-write-from-googleform-http-type #152

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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/actiontable_write_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/actiontable-write-from-googleform

## category

アクションテーブル,Craft Functions,Google Form,CRAFT_ENDPOINT
アクションテーブル,Craft Functions,Google Form,CRAFT_ENDPOINT

## functionType

http
63 changes: 38 additions & 25 deletions craft-functions/actiontable_write_from_google_form/index.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,44 @@
import api from 'api';
const LOG_LEVEL = '<% LOG_LEVEL %>'; // ログのレベルを必要に応じて定義
const SECRET_NAME = '<% SECRET_NAME %>'; // 登録したシークレットを定義
const karteApiClient = api('@dev-karte/v1.0#1ehqt16lkm2a8jw');
const TABLE_ID = '<% TABLE_ID %>' // 今回更新したいテーブルIDを登録

const LOG_LEVEL = '<% LOG_LEVEL %>';
const SECRET_NAME = '<% SECRET_NAME %>';
const karteApiClient = api('@dev-karte/v1.0#1ehqt16lkm2a8jw');
const TABLE_ID = '<% TABLE_ID %>';

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];
karteApiClient.auth(token);
const { initLogger, secret } = MODULES;
const { req, res } = data;
const logger = initLogger({ logLevel: LOG_LEVEL });
const secrets = await secret.get({ keys: [SECRET_NAME] });
const token = secrets[SECRET_NAME];
karteApiClient.auth(token);

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 pageUrl = data.jsonPayload.data.hook_data.body.PageURL; // body.xxxの部分はGAS側の記述に依存して変更可能
const salesCount = data.jsonPayload.data.hook_data.body.SalesCount;
const others = data.jsonPayload.data.hook_data.body.Others;
const pageUrl = req.body.PageURL;
const salesCount = req.body.SalesCount;
const others = req.body.Others;

try {
await karteApiClient.postV2betaActionActiontableRecordsUpsert({
table: TABLE_ID,
data: {
pageurl: pageUrl, // ここの内容は作成したアクションテーブルのカラム名に応じて変更可能、今回の場合はpageurl, salescount, othersの3カラムにしたので左記のようになっています。
salescount: salesCount,
others
}
});
logger.log(`${TABLE_ID} is updated`);
} catch (e) {
logger.error(e);
}
try {
await karteApiClient.postV2betaActionActiontableRecordsUpsert({
table: TABLE_ID,
data: {
pageurl: pageUrl,
salescount: salesCount,
others,
},
});
logger.log(`${TABLE_ID} is updated`);
res.status(200).json({ message: 'Success' });
} catch (e) {
logger.error(e);
res.status(500).json({ message: 'Error occurred' });
}
}