From 3bc81c5c1ea35c7e0a7bb2014fd8e614fc9ba95f Mon Sep 17 00:00:00 2001 From: soy Date: Sun, 7 Jul 2024 11:28:54 +0900 Subject: [PATCH 1/2] refactor: lottery-with-limits-of-wins-http-type --- .../lottery-with-limits-of-wins/README.md | 4 ++ .../lottery-with-limits-of-wins/index.js | 53 +++++++++---------- 2 files changed, 29 insertions(+), 28 deletions(-) diff --git a/craft-functions/lottery-with-limits-of-wins/README.md b/craft-functions/lottery-with-limits-of-wins/README.md index aebd274..6ffd882 100644 --- a/craft-functions/lottery-with-limits-of-wins/README.md +++ b/craft-functions/lottery-with-limits-of-wins/README.md @@ -15,3 +15,7 @@ KARTE の接客サービスと Craft を組み合わせて、上限設定付き ## category Craft Functions,Craft KVS,Craft Counter,KARTEイベント,CRAFT_ENDPOINT + +## functionType + +http \ No newline at end of file diff --git a/craft-functions/lottery-with-limits-of-wins/index.js b/craft-functions/lottery-with-limits-of-wins/index.js index 30c05d6..eea0b4e 100644 --- a/craft-functions/lottery-with-limits-of-wins/index.js +++ b/craft-functions/lottery-with-limits-of-wins/index.js @@ -224,50 +224,45 @@ async function determinePrize(rand, probabilities) { * name or an error message. */ export default async function (data, { MODULES }) { + const { req, res } = data; const { kvs, counter, initLogger, secret } = MODULES; const logger = initLogger({ logLevel: LOG_LEVEL }); const secrets = await secret.get({ keys: [KARTE_APP_TOKEN_SECRET] }); const token = secrets[KARTE_APP_TOKEN_SECRET]; + 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 { logger.debug('Starting the lucky draw process.'); if (PRIZES.length !== LIMITS.length) { const errorMessage = 'PRIZES and LIMITS must have the same length.'; logger.error(errorMessage); - return { craft_status_code: 500, error: errorMessage }; + res.status(500).send({ error: errorMessage }); + return; } - if (data.kind !== 'karte/track-hook') { - logger.error('Invalid request type. Expected "karte/track-hook".'); - return { craft_status_code: 400, error: 'Invalid request type' }; - } - - const { jsonPayload } = data; - if (!jsonPayload?.data?.hook_data?.body) { - logger.error('Invalid payload: Missing required data'); - return { craft_status_code: 400, error: 'Invalid payload' }; - } - - const { - body: { lotteryKey, userId }, - } = jsonPayload.data.hook_data; + const { body } = req; + const { lotteryKey, userId } = body; if (!lotteryKey || !userId) { const missingKeyError = `Missing ${!lotteryKey ? 'lotteryKey' : 'userId'}`; logger.warn(missingKeyError); - return { - craft_status_code: 400, - error: `${missingKeyError} is required.`, - }; + res.status(400).send({ error: `${missingKeyError} is required.` }); + return; } const hasParticipated = await hasParticipatedRecently({ lotteryKey, userId, kvs, logger }); if (hasParticipated) { logger.debug(`User ${userId} has participated recently.`); - return { - craft_status_code: 400, - error: 'User has participated recently.', - }; + res.status(400).send({ error: 'User has participated recently.' }); + return; } const rand = Math.random(); @@ -289,7 +284,8 @@ export default async function (data, { MODULES }) { }); await setParticipationTime({ lotteryKey, userId, kvs, logger }); logger.debug(`User ${userId} did not win any prize.`); - return { craft_status_code: 200, result: 'No prize won' }; + res.status(200).send({ result: 'No prize won' }); + return; } const { prize, index } = prizeResult; @@ -310,15 +306,16 @@ export default async function (data, { MODULES }) { logger, }); logger.debug(`Prize ${prize} has reached its limit.`); - return { craft_status_code: 200, result: 'No prize won' }; + res.status(200).send({ result: 'No prize won' }); + return; } await sendKarteEvent({ userId, lotteryKey, prize, message: '', token, logger }); await setParticipationTime({ lotteryKey, userId, kvs, logger }); logger.debug(`User ${userId} won prize: ${prize}. ${LIMITS[index] - count} left.`); - return { craft_status_code: 200, result: prize }; + res.status(200).send({ result: prize }); } catch (error) { logger.error(`Error in the lucky draw process: ${error.toString()}`); - return { craft_status_code: 500, error: `Internal Server Error: ${error.message}` }; + res.status(500).send({ error: `Internal Server Error: ${error.message}` }); } -} +} \ No newline at end of file From 09030e1a5bedb345fa0f49f45a789c38c8a027cb Mon Sep 17 00:00:00 2001 From: soy Date: Tue, 9 Jul 2024 06:51:40 +0900 Subject: [PATCH 2/2] fix: res.send to res.json --- .../lottery-with-limits-of-wins/index.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/craft-functions/lottery-with-limits-of-wins/index.js b/craft-functions/lottery-with-limits-of-wins/index.js index eea0b4e..f39bcea 100644 --- a/craft-functions/lottery-with-limits-of-wins/index.js +++ b/craft-functions/lottery-with-limits-of-wins/index.js @@ -245,7 +245,7 @@ export default async function (data, { MODULES }) { if (PRIZES.length !== LIMITS.length) { const errorMessage = 'PRIZES and LIMITS must have the same length.'; logger.error(errorMessage); - res.status(500).send({ error: errorMessage }); + res.status(500).json({ error: errorMessage }); return; } @@ -254,14 +254,14 @@ export default async function (data, { MODULES }) { if (!lotteryKey || !userId) { const missingKeyError = `Missing ${!lotteryKey ? 'lotteryKey' : 'userId'}`; logger.warn(missingKeyError); - res.status(400).send({ error: `${missingKeyError} is required.` }); + res.status(400).json({ error: `${missingKeyError} is required.` }); return; } const hasParticipated = await hasParticipatedRecently({ lotteryKey, userId, kvs, logger }); if (hasParticipated) { logger.debug(`User ${userId} has participated recently.`); - res.status(400).send({ error: 'User has participated recently.' }); + res.status(400).json({ error: 'User has participated recently.' }); return; } @@ -284,7 +284,7 @@ export default async function (data, { MODULES }) { }); await setParticipationTime({ lotteryKey, userId, kvs, logger }); logger.debug(`User ${userId} did not win any prize.`); - res.status(200).send({ result: 'No prize won' }); + res.status(200).json({ result: 'No prize won' }); return; } @@ -306,16 +306,16 @@ export default async function (data, { MODULES }) { logger, }); logger.debug(`Prize ${prize} has reached its limit.`); - res.status(200).send({ result: 'No prize won' }); + res.status(200).json({ result: 'No prize won' }); return; } await sendKarteEvent({ userId, lotteryKey, prize, message: '', token, logger }); await setParticipationTime({ lotteryKey, userId, kvs, logger }); logger.debug(`User ${userId} won prize: ${prize}. ${LIMITS[index] - count} left.`); - res.status(200).send({ result: prize }); + res.status(200).json({ result: prize }); } catch (error) { logger.error(`Error in the lucky draw process: ${error.toString()}`); - res.status(500).send({ error: `Internal Server Error: ${error.message}` }); + res.status(500).json({ error: `Internal Server Error: ${error.message}` }); } } \ No newline at end of file