From 5a5fb5051aef9d0b6333882179c9a634a7ac4fba Mon Sep 17 00:00:00 2001 From: RexBearIU Date: Thu, 26 Sep 2024 17:50:20 +0800 Subject: [PATCH] feat: modify url path and http method --- package.json | 2 +- .../[accountingSettingId]/index.ts | 73 --------------- .../[companyId]/accounting_setting/index.ts | 31 ++++--- .../[companySettingId]/index.ts | 69 -------------- .../[userId]/{login_info.ts => action_log.ts} | 0 .../user_setting/[userSettingId]/index.ts | 93 ------------------- .../v2/user/[userId]/user_setting/index.ts | 25 ++--- 7 files changed, 32 insertions(+), 261 deletions(-) delete mode 100644 src/pages/api/v2/company/[companyId]/accounting_setting/[accountingSettingId]/index.ts delete mode 100644 src/pages/api/v2/company/[companyId]/company_setting/[companySettingId]/index.ts rename src/pages/api/v2/user/[userId]/{login_info.ts => action_log.ts} (100%) delete mode 100644 src/pages/api/v2/user/[userId]/user_setting/[userSettingId]/index.ts diff --git a/package.json b/package.json index 1afbdb23..c00756e2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "iSunFA", - "version": "0.8.2+15", + "version": "0.8.2+16", "private": false, "scripts": { "dev": "next dev", diff --git a/src/pages/api/v2/company/[companyId]/accounting_setting/[accountingSettingId]/index.ts b/src/pages/api/v2/company/[companyId]/accounting_setting/[accountingSettingId]/index.ts deleted file mode 100644 index b52eb5c6..00000000 --- a/src/pages/api/v2/company/[companyId]/accounting_setting/[accountingSettingId]/index.ts +++ /dev/null @@ -1,73 +0,0 @@ -import { STATUS_MESSAGE } from '@/constants/status_code'; -import { IResponseData } from '@/interfaces/response_data'; -import { IAccountingSetting } from '@/interfaces/accounting_setting'; -import { formatApiResponse } from '@/lib/utils/common'; -import { NextApiRequest, NextApiResponse } from 'next'; - -// ToDo: (20240924 - Jacky) Implement the logic to update an existing accounting setting in the database -async function handlePutRequest(req: NextApiRequest) { - let statusMessage: string = STATUS_MESSAGE.BAD_REQUEST; - let payload: IAccountingSetting | null = null; - - // ToDo: (20240924 - Jacky) Get session data from the request - // ToDo: (20240924 - Jacky) Check if the user is authorized to access this API - // ToDo: (20240924 - Jacky) Validate the request body - // ToDo: (20240924 - Jacky) Implement the logic to update an existing accounting setting in the database - // ToDo: (20240924 - Jacky) Format the accounting settings data to the IAccountingSetting interface - - // Deprecated: (20240924 - Jacky) Mock data for connection - const { accountingSettingId } = req.query; - const { companyId, companyName, taxSettings, currency, lastDayOfFiscalYear, shortcutList } = - req.body; - - const updatedAccountingSetting: IAccountingSetting = { - id: Number(accountingSettingId), - companyId, - companyName, - taxSettings, - currency, - lastDayOfFiscalYear, - shortcutList, - }; - - payload = updatedAccountingSetting; - statusMessage = STATUS_MESSAGE.SUCCESS_UPDATE; - - return { statusMessage, payload }; -} - -const methodHandlers: { - [key: string]: ( - req: NextApiRequest, - res: NextApiResponse - ) => Promise<{ statusMessage: string; payload: IAccountingSetting | null }>; -} = { - PUT: handlePutRequest, -}; - -export default async function handler( - req: NextApiRequest, - res: NextApiResponse> -) { - let statusMessage: string = STATUS_MESSAGE.BAD_REQUEST; - let payload: IAccountingSetting | null = null; - - try { - const handleRequest = methodHandlers[req.method || '']; - if (handleRequest) { - ({ statusMessage, payload } = await handleRequest(req, res)); - } else { - statusMessage = STATUS_MESSAGE.METHOD_NOT_ALLOWED; - } - } catch (_error) { - const error = _error as Error; - statusMessage = error.message; - payload = null; - } finally { - const { httpCode, result } = formatApiResponse( - statusMessage, - payload - ); - res.status(httpCode).json(result); - } -} diff --git a/src/pages/api/v2/company/[companyId]/accounting_setting/index.ts b/src/pages/api/v2/company/[companyId]/accounting_setting/index.ts index 728e602b..848325a2 100644 --- a/src/pages/api/v2/company/[companyId]/accounting_setting/index.ts +++ b/src/pages/api/v2/company/[companyId]/accounting_setting/index.ts @@ -42,30 +42,33 @@ async function handleGetRequest() { return { statusMessage, payload }; } -// ToDo: (20240924 - Jacky) Implement the logic to create a new accounting setting in the database -async function handlePostRequest(req: NextApiRequest) { +// ToDo: (20240924 - Jacky) Implement the logic to update an existing accounting setting in the database +async function handlePutRequest(req: NextApiRequest) { let statusMessage: string = STATUS_MESSAGE.BAD_REQUEST; let payload: IAccountingSetting | null = null; // ToDo: (20240924 - Jacky) Get session data from the request // ToDo: (20240924 - Jacky) Check if the user is authorized to access this API // ToDo: (20240924 - Jacky) Validate the request body - // ToDo: (20240924 - Jacky) Implement the logic to create a new accounting setting in the database + // ToDo: (20240924 - Jacky) Implement the logic to update an existing accounting setting in the database // ToDo: (20240924 - Jacky) Format the accounting settings data to the IAccountingSetting interface // Deprecated: (20240924 - Jacky) Mock data for connection - const newSetting: IAccountingSetting = { - id: 1, - companyId: 2, - companyName: req.body.companyName, - taxSettings: req.body.taxSettings, - currency: req.body.currency, - lastDayOfFiscalYear: req.body.lastDayOfFiscalYear, - shortcutList: req.body.shortcutList, + const { companyId, companyName, taxSettings, currency, lastDayOfFiscalYear, shortcutList } = + req.body; + + const updatedAccountingSetting: IAccountingSetting = { + id: 123, + companyId, + companyName, + taxSettings, + currency, + lastDayOfFiscalYear, + shortcutList, }; - payload = newSetting; - statusMessage = STATUS_MESSAGE.CREATED; + payload = updatedAccountingSetting; + statusMessage = STATUS_MESSAGE.SUCCESS_UPDATE; return { statusMessage, payload }; } @@ -77,7 +80,7 @@ const methodHandlers: { ) => Promise<{ statusMessage: string; payload: IAccountingSetting | null }>; } = { GET: handleGetRequest, - POST: handlePostRequest, + PUT: handlePutRequest, }; export default async function handler( diff --git a/src/pages/api/v2/company/[companyId]/company_setting/[companySettingId]/index.ts b/src/pages/api/v2/company/[companyId]/company_setting/[companySettingId]/index.ts deleted file mode 100644 index 41c32484..00000000 --- a/src/pages/api/v2/company/[companyId]/company_setting/[companySettingId]/index.ts +++ /dev/null @@ -1,69 +0,0 @@ -import { STATUS_MESSAGE } from '@/constants/status_code'; -import { IResponseData } from '@/interfaces/response_data'; -import { ICompanySetting } from '@/interfaces/company_setting'; -import { formatApiResponse } from '@/lib/utils/common'; -import { NextApiRequest, NextApiResponse } from 'next'; - -// ToDo: (20240924 - Jacky) Implement the logic to update an existing company setting in the database -async function handlePutRequest(req: NextApiRequest) { - let statusMessage: string = STATUS_MESSAGE.BAD_REQUEST; - let payload: ICompanySetting | null = null; - - // ToDo: (20240924 - Jacky) Get session data from the request - // ToDo: (20240924 - Jacky) Check if the user is authorized to access this API - // ToDo: (20240924 - Jacky) Validate the request body - // ToDo: (20240924 - Jacky) Implement the logic to update an existing company setting in the database - // ToDo: (20240924 - Jacky) Format the company settings data to the ICompanySetting interface - - // Deprecated: (20240924 - Jacky) Mock data for connection - const { companySettingId } = req.query; - const { companyId, taxSerialNumber, representativeName, country, phone, address } = req.body; - - const updatedCompanySetting: ICompanySetting = { - id: Number(companySettingId), - companyId, - taxSerialNumber, - representativeName, - country, - phone, - address, - }; - - payload = updatedCompanySetting; - statusMessage = STATUS_MESSAGE.SUCCESS_UPDATE; - - return { statusMessage, payload }; -} - -const methodHandlers: { - [key: string]: ( - req: NextApiRequest, - res: NextApiResponse - ) => Promise<{ statusMessage: string; payload: ICompanySetting | null }>; -} = { - PUT: handlePutRequest, -}; - -export default async function handler( - req: NextApiRequest, - res: NextApiResponse> -) { - let statusMessage: string = STATUS_MESSAGE.BAD_REQUEST; - let payload: ICompanySetting | null = null; - - try { - const handleRequest = methodHandlers[req.method || '']; - if (handleRequest) { - ({ statusMessage, payload } = await handleRequest(req, res)); - } else { - statusMessage = STATUS_MESSAGE.METHOD_NOT_ALLOWED; - } - } catch (_error) { - const error = _error as Error; - statusMessage = error.message; - payload = null; - } finally { - const { httpCode, result } = formatApiResponse(statusMessage, payload); - res.status(httpCode).json(result); - } -} diff --git a/src/pages/api/v2/user/[userId]/login_info.ts b/src/pages/api/v2/user/[userId]/action_log.ts similarity index 100% rename from src/pages/api/v2/user/[userId]/login_info.ts rename to src/pages/api/v2/user/[userId]/action_log.ts diff --git a/src/pages/api/v2/user/[userId]/user_setting/[userSettingId]/index.ts b/src/pages/api/v2/user/[userId]/user_setting/[userSettingId]/index.ts deleted file mode 100644 index 7969fa3a..00000000 --- a/src/pages/api/v2/user/[userId]/user_setting/[userSettingId]/index.ts +++ /dev/null @@ -1,93 +0,0 @@ -import { STATUS_MESSAGE } from '@/constants/status_code'; -import { IResponseData } from '@/interfaces/response_data'; -import { IUserSetting } from '@/interfaces/user_setting'; -import { formatApiResponse, timestampInSeconds } from '@/lib/utils/common'; -import { NextApiRequest, NextApiResponse } from 'next'; - -// ToDo: (20240924 - Jacky) Implement the logic to update an existing user setting in the database -async function handlePutRequest(req: NextApiRequest) { - let statusMessage: string = STATUS_MESSAGE.BAD_REQUEST; - let payload: IUserSetting | null = null; - - // ToDo: (20240924 - Jacky) Get session data from the request - // ToDo: (20240924 - Jacky) Check if the user is authorized to access this API - // ToDo: (20240924 - Jacky) Validate the request body - // ToDo: (20240924 - Jacky) Implement the logic to update an existing user setting in the database - // ToDo: (20240924 - Jacky) Format the user settings data to the IUserSetting interface - - // Deprecated: (20240924 - Jacky) Mock data for connection - const { userSettingId } = req.query; - const { - userId, - firstName, - lastName, - country, - phone, - language, - systemNotification, - updateAndSubscriptionNotification, - emailNotification, - createdAt, - deletedAt, - } = req.body; - - const now = Date.now(); - const nowTimestamp = timestampInSeconds(now); - const updatedIUserSetting: IUserSetting = { - id: Number(userSettingId), - userId, - personalInfo: { - firstName, - lastName, - country, - phone, - language, - }, - notificationSetting: { - systemNotification, - updateAndSubscriptionNotification, - emailNotification, - }, - createdAt, - updatedAt: nowTimestamp, - deletedAt, - }; - - payload = updatedIUserSetting; - statusMessage = STATUS_MESSAGE.SUCCESS_UPDATE; - - return { statusMessage, payload }; -} - -const methodHandlers: { - [key: string]: ( - req: NextApiRequest, - res: NextApiResponse - ) => Promise<{ statusMessage: string; payload: IUserSetting | null }>; -} = { - PUT: handlePutRequest, -}; - -export default async function handler( - req: NextApiRequest, - res: NextApiResponse> -) { - let statusMessage: string = STATUS_MESSAGE.BAD_REQUEST; - let payload: IUserSetting | null = null; - - try { - const handleRequest = methodHandlers[req.method || '']; - if (handleRequest) { - ({ statusMessage, payload } = await handleRequest(req, res)); - } else { - statusMessage = STATUS_MESSAGE.METHOD_NOT_ALLOWED; - } - } catch (_error) { - const error = _error as Error; - statusMessage = error.message; - payload = null; - } finally { - const { httpCode, result } = formatApiResponse(statusMessage, payload); - res.status(httpCode).json(result); - } -} diff --git a/src/pages/api/v2/user/[userId]/user_setting/index.ts b/src/pages/api/v2/user/[userId]/user_setting/index.ts index bb50471d..7dcd3303 100644 --- a/src/pages/api/v2/user/[userId]/user_setting/index.ts +++ b/src/pages/api/v2/user/[userId]/user_setting/index.ts @@ -39,19 +39,20 @@ async function handleGetRequest() { return { statusMessage, payload }; } -// ToDo: (20240924 - Jacky) Implement the logic to create a new user setting in the database -async function handlePostRequest(req: NextApiRequest) { +// ToDo: (20240924 - Jacky) Implement the logic to update an existing user setting in the database +async function handlePutRequest(req: NextApiRequest) { let statusMessage: string = STATUS_MESSAGE.BAD_REQUEST; let payload: IUserSetting | null = null; // ToDo: (20240924 - Jacky) Get session data from the request // ToDo: (20240924 - Jacky) Check if the user is authorized to access this API // ToDo: (20240924 - Jacky) Validate the request body - // ToDo: (20240924 - Jacky) Implement the logic to create a new user setting in the database + // ToDo: (20240924 - Jacky) Implement the logic to update an existing user setting in the database // ToDo: (20240924 - Jacky) Format the user settings data to the IUserSetting interface // Deprecated: (20240924 - Jacky) Mock data for connection const { + userId, firstName, lastName, country, @@ -60,13 +61,15 @@ async function handlePostRequest(req: NextApiRequest) { systemNotification, updateAndSubscriptionNotification, emailNotification, + createdAt, + deletedAt, } = req.body; const now = Date.now(); const nowTimestamp = timestampInSeconds(now); - const newIUserSetting: IUserSetting = { - id: 3, - userId: req.body.userId, // ToDo: (20240926 - Jacky) Get the user ID from the session + const updatedIUserSetting: IUserSetting = { + id: 1, + userId, personalInfo: { firstName, lastName, @@ -79,13 +82,13 @@ async function handlePostRequest(req: NextApiRequest) { updateAndSubscriptionNotification, emailNotification, }, - createdAt: nowTimestamp, + createdAt, updatedAt: nowTimestamp, - deletedAt: 0, + deletedAt, }; - payload = newIUserSetting; - statusMessage = STATUS_MESSAGE.CREATED; + payload = updatedIUserSetting; + statusMessage = STATUS_MESSAGE.SUCCESS_UPDATE; return { statusMessage, payload }; } @@ -97,7 +100,7 @@ const methodHandlers: { ) => Promise<{ statusMessage: string; payload: IUserSetting | null }>; } = { GET: handleGetRequest, - POST: handlePostRequest, + PUT: handlePutRequest, }; export default async function handler(