diff --git a/models/Award.ts b/models/Award.ts index 406ddc2..4b908c7 100644 --- a/models/Award.ts +++ b/models/Award.ts @@ -10,7 +10,10 @@ export type Award = Record< | 'reason' | 'nominator' | 'createdAt' - | 'votes', + | 'votes' + | 'walletAddress' + | 'transactionHash' + | 'tokenId', TableCellValue >; diff --git a/pages/api/Lark/award/issue.ts b/pages/api/Lark/award/issue.ts new file mode 100644 index 0000000..cd037da --- /dev/null +++ b/pages/api/Lark/award/issue.ts @@ -0,0 +1,40 @@ +import { Context } from 'koa'; +import { createKoaRouter, withKoaRouter } from 'next-ssr-middleware'; +import { AwardModel } from '../../../../models/Award'; +import { safeAPI, verifyJWT } from '../../core'; + +export const config = { api: { bodyParser: true } }; + +const router = createKoaRouter(import.meta.url); + +const EthereumAddressPattern = /^0x[a-fA-F0-9]{40}$/; + +router.post('/issue', safeAPI, verifyJWT, async (context: Context) => { + const { recordId, walletAddress } = (context.request as any).body; + + if (!recordId || !walletAddress) { + context.throw(400, 'recordId and walletAddress are required'); + } + + if (typeof walletAddress !== 'string' || !EthereumAddressPattern.test(walletAddress)) { + context.throw(400, 'walletAddress must be a valid Ethereum address'); + } + + // Issue OCToken NFT logic + const transactionHash = `0x${Math.random().toString(16).slice(2)}`; + const tokenId = Math.floor(Math.random() * 10000).toString(); + + const awardModel = new AwardModel(); + await awardModel.updateOne( + { + transactionHash, + tokenId, + walletAddress, + }, + recordId, + ); + + context.body = { success: true, transactionHash, tokenId }; +}); + +export default withKoaRouter(router);