Skip to content

Commit

Permalink
feat: mock remain api
Browse files Browse the repository at this point in the history
  • Loading branch information
RexBearIU committed Sep 26, 2024
1 parent c8813c2 commit 1ad37f0
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 15 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "iSunFA",
"version": "0.8.2+12",
"version": "0.8.2+13",
"private": false,
"scripts": {
"dev": "next dev",
Expand Down
39 changes: 27 additions & 12 deletions src/interfaces/pending_task.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
// export interface IPendingTask {
// id: number;
// userId: number;
// taskType: string;
// taskDescription: string;
// taskTime: number;
// taskStatus: string;
// taskPriority: string;
// createdAt: number;
// updatedAt: number;
// deletedAt: number;
// }
export interface IPendingTask {
id: number;
companyId: number;
missingCertificate: IMissingCertificate;
unpostedVoucher: IUnpostedVoucher;
}

interface IMissingCertificate {
id: number;
companyId: number;
count: number;
}

interface IUnpostedVoucher {
id: number;
companyId: number;
count: number;
}

export interface IPendingTaskTotal {
id: number;
userId: number;
totalMissingCertificate: number;
missingCertificateList: IMissingCertificate[];
totalUnpostedVoucher: number;
unpostedVoucherList: IUnpostedVoucher[];
}
68 changes: 68 additions & 0 deletions src/pages/api/v2/company/[companyId]/pending_task/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { STATUS_MESSAGE } from '@/constants/status_code';
import { IResponseData } from '@/interfaces/response_data';
import { formatApiResponse } from '@/lib/utils/common';
import { NextApiRequest, NextApiResponse } from 'next';
import { IPendingTask } from '@/interfaces/pending_task';

// ToDo: (20240924 - Jacky) Implement the logic to get the pending tasks data from the database
async function handleGetRequest() {
let statusMessage: string = STATUS_MESSAGE.BAD_REQUEST;
let payload: IPendingTask | 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) Implement the logic to get the pending tasks data from the database
// ToDo: (20240924 - Jacky) Format the pending tasks data to the IPendingTaskTotal interface

// Deprecated: (20240924 - Jacky) Mock data for connection
payload = {
id: 1,
companyId: 1,
missingCertificate: {
id: 1,
companyId: 1,
count: 2,
},
unpostedVoucher: {
id: 1,
companyId: 1,
count: 3,
},
};
statusMessage = STATUS_MESSAGE.SUCCESS_LIST;

return { statusMessage, payload };
}

const methodHandlers: {
[key: string]: (
req: NextApiRequest,
res: NextApiResponse
) => Promise<{ statusMessage: string; payload: IPendingTask | null }>;
} = {
GET: handleGetRequest,
};

export default async function handler(
req: NextApiRequest,
res: NextApiResponse<IResponseData<IPendingTask | null>>
) {
let statusMessage: string = STATUS_MESSAGE.BAD_REQUEST;
let payload: IPendingTask | 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<IPendingTask | null>(statusMessage, payload);
res.status(httpCode).json(result);
}
}
4 changes: 2 additions & 2 deletions src/pages/api/v2/company/[companyId]/setting/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ async function handleGetRequest() {
}

// ToDo: (20240924 - Jacky) Implement the logic to create a new company setting in the database
async function handlePostRequest(req: NextApiRequest) {
async function handlePutRequest(req: NextApiRequest) {
let statusMessage: string = STATUS_MESSAGE.BAD_REQUEST;
let payload: ICompanySetting | null = null;

Expand Down Expand Up @@ -65,7 +65,7 @@ const methodHandlers: {
) => Promise<{ statusMessage: string; payload: ICompanySetting | null }>;
} = {
GET: handleGetRequest,
POST: handlePostRequest,
PUT: handlePutRequest,
};

// Main handler function to route requests to the appropriate handler
Expand Down
71 changes: 71 additions & 0 deletions src/pages/api/v2/user/[userId]/pending_task.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { STATUS_MESSAGE } from '@/constants/status_code';
import { IResponseData } from '@/interfaces/response_data';
import { formatApiResponse } from '@/lib/utils/common';
import { NextApiRequest, NextApiResponse } from 'next';
import { IPendingTask, IPendingTaskTotal } from '@/interfaces/pending_task';

// ToDo: (20240924 - Jacky) Implement the logic to get the pending tasks data from the database
async function handleGetRequest() {
let statusMessage: string = STATUS_MESSAGE.BAD_REQUEST;
let payload: IPendingTaskTotal | 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) Implement the logic to get the pending tasks data from the database
// ToDo: (20240924 - Jacky) Format the pending tasks data to the IPendingTaskTotal interface

// Deprecated: (20240924 - Jacky) Mock data for connection
payload = {
id: 1,
userId: 1,
totalMissingCertificate: 5,
missingCertificateList: [
{ id: 1, companyId: 1, count: 2 },
{ id: 2, companyId: 1, count: 3 },
],
totalUnpostedVoucher: 3,
unpostedVoucherList: [
{ id: 1, companyId: 1, count: 1 },
{ id: 2, companyId: 1, count: 2 },
],
};
statusMessage = STATUS_MESSAGE.SUCCESS_LIST;

return { statusMessage, payload };
}

const methodHandlers: {
[key: string]: (
req: NextApiRequest,
res: NextApiResponse
) => Promise<{ statusMessage: string; payload: IPendingTask | IPendingTaskTotal | null }>;
} = {
GET: handleGetRequest,
};

export default async function handler(
req: NextApiRequest,
res: NextApiResponse<IResponseData<IPendingTask | IPendingTaskTotal | null>>
) {
let statusMessage: string = STATUS_MESSAGE.BAD_REQUEST;
let payload: IPendingTask | IPendingTaskTotal | 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<IPendingTask | IPendingTaskTotal | null>(
statusMessage,
payload
);
res.status(httpCode).json(result);
}
}

0 comments on commit 1ad37f0

Please sign in to comment.