Skip to content

Commit

Permalink
Merge pull request #2671 from CAFECA-IO/feature/remain_mock
Browse files Browse the repository at this point in the history
Feature/remain mock
  • Loading branch information
Luphia authored Sep 26, 2024
2 parents ee03f92 + c8f52a1 commit d3862bb
Show file tree
Hide file tree
Showing 15 changed files with 581 additions and 168 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
1 change: 1 addition & 0 deletions src/interfaces/accounting_setting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ interface IShortcut {
}

export interface IAccountingSetting {
id: number;
companyId: number;
companyName: string;
taxSettings: ITaxSetting;
Expand Down
3 changes: 1 addition & 2 deletions src/interfaces/company.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ export interface ICompany {

export interface ICompanyBeta {
id: number;
taxId: string;
imageId: string;
name: string;
regional: string;
taxId: string;
tag: string;
startDate: number;
createdAt: number;
Expand Down
11 changes: 5 additions & 6 deletions src/interfaces/company_setting.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
export interface ICompanySetting {
id: number;
companyId: number;
autoRenewal: boolean;
notifyTiming: number;
notifyChannel: string;
reminderFreq: number;
createdAt: number;
updatedAt: number;
taxSerialNumber: string;
representativeName: string;
country: string;
phone: string;
address: string;
}
27 changes: 27 additions & 0 deletions src/interfaces/pending_task.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
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[];
}
21 changes: 14 additions & 7 deletions src/interfaces/user_setting.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
export interface IUserSetting {
id: number;
userId: number;
personalInfo: IUserPersonalInfo;
notificationSetting: INotificationSetting;
createdAt: number;
updatedAt: number;
deletedAt: number;
}
interface INotificationSetting {
systemNotification: boolean;
updateAndSubscriptionNotification: boolean;
emailNotification: boolean;
}

interface IUserPersonalInfo {
firstName: string;
lastName: string;
country: string;
phone: string;
language: string;
systemNotification: boolean;
updateAndSubscriptionNotification: boolean;
emailNotification: boolean;
createdAt: number;
updatedAt: number;
deletedAt: number;
phone: string;
}
31 changes: 18 additions & 13 deletions src/pages/api/v2/company/[companyId]/accounting_setting/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ async function handleGetRequest() {

// Deprecated: (20240924 - Jacky) Mock data for connection
payload = {
id: 1,
companyId: 1,
companyName: 'Company A',
taxSettings: {
Expand All @@ -41,29 +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 = {
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 };
}
Expand All @@ -75,7 +80,7 @@ const methodHandlers: {
) => Promise<{ statusMessage: string; payload: IAccountingSetting | null }>;
} = {
GET: handleGetRequest,
POST: handlePostRequest,
PUT: handlePutRequest,
};

export default async function handler(
Expand Down
96 changes: 96 additions & 0 deletions src/pages/api/v2/company/[companyId]/company_setting/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
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 { ICompanySetting } from '@/interfaces/company_setting';

// ToDo: (20240924 - Jacky) Implement the logic to get the company settings data from the database
async function handleGetRequest() {
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) Implement the logic to get the company settings data from the database
// ToDo: (20240924 - Jacky) Format the company settings data to the ICompanySetting interface

// Deprecated: (20240924 - Jacky) Mock data for connection
payload = {
id: 1,
companyId: 1,
taxSerialNumber: '123456789',
representativeName: 'John Doe',
country: 'USA',
phone: '123-456-7890',
address: '123 Main St, Anytown, USA',
};
statusMessage = STATUS_MESSAGE.SUCCESS_LIST;

return { statusMessage, payload };
}

// ToDo: (20240924 - Jacky) Implement the logic to create a new 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 create a new 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 newSetting: ICompanySetting = {
id: 2,
companyId: req.body.companyId,
taxSerialNumber: req.body.taxSerialNumber,
representativeName: req.body.representativeName,
country: req.body.country,
phone: req.body.phone,
address: req.body.address,
};

payload = newSetting;
statusMessage = STATUS_MESSAGE.CREATED;

return { statusMessage, payload };
}

// Define method handlers for different HTTP methods
const methodHandlers: {
[key: string]: (
req: NextApiRequest,
res: NextApiResponse
) => Promise<{ statusMessage: string; payload: ICompanySetting | null }>;
} = {
GET: handleGetRequest,
PUT: handlePutRequest,
};

// Main handler function to route requests to the appropriate handler
export default async function handler(
req: NextApiRequest,
res: NextApiResponse<IResponseData<ICompanySetting | null>>
) {
let statusMessage: string = STATUS_MESSAGE.BAD_REQUEST;
let payload: ICompanySetting | null = null;

try {
// Determine the appropriate handler based on the HTTP method
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 {
// Format and send the response
const { httpCode, result } = formatApiResponse<ICompanySetting | null>(statusMessage, payload);
res.status(httpCode).json(result);
}
}
79 changes: 48 additions & 31 deletions src/pages/api/v2/company/[companyId]/customer_vendor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import { ICustomerVendor } from '@/interfaces/customer_vendor';
import { IResponseData } from '@/interfaces/response_data';
import { STATUS_MESSAGE } from '@/constants/status_code';
import { formatApiResponse } from '@/lib/utils/common';
import { IPaginatedData } from '@/interfaces/pagination';

// ToDo: (20240925 - Jacky) Implement the logic to get the customer/vendor data from the database
async function handleGetRequest() {
let statusMessage: string = STATUS_MESSAGE.BAD_REQUEST;
let payload: ICustomerVendor | ICustomerVendor[] | null = null;
let payload: IPaginatedData<ICustomerVendor[]> | null = null;
// ToDo: (20240925 - Jacky) Get session data from the request
// const session = await getSession(req, res);
// const { userId, companyId } = session;
Expand All @@ -23,28 +24,42 @@ async function handleGetRequest() {
// if (!isAuth) {
// statusMessage = STATUS_MESSAGE.FORBIDDEN;
// } else {
const customerVendorList: ICustomerVendor[] = [
{
id: 1,
companyId: 1,
name: 'Test Customer',
taxId: '123456',
type: 'Customer',
note: 'Test Note',
createdAt: 123456,
updatedAt: 123456,
},
{
id: 2,
companyId: 1,
name: 'Test Vendor',
taxId: '123456',
type: 'Vendor',
note: 'Test Note',
createdAt: 123456,
updatedAt: 123456,
},
];
const customerVendorList: IPaginatedData<ICustomerVendor[]> = {
data: [
{
id: 1,
companyId: 1,
name: 'Test Customer',
taxId: '123456',
type: 'Customer',
note: 'Test Note',
createdAt: 123456,
updatedAt: 123456,
},
{
id: 2,
companyId: 1,
name: 'Test Vendor',
taxId: '123456',
type: 'Vendor',
note: 'Test Note',
createdAt: 123456,
updatedAt: 123456,
},
],
page: 1,
totalPages: 5,
totalCount: 23,
pageSize: 5,
hasNextPage: true,
hasPreviousPage: false,
sort: [
{
sortBy: 'createdAt',
sortOrder: 'desc',
},
],
};
statusMessage = STATUS_MESSAGE.SUCCESS_LIST;
payload = customerVendorList;
// }
Expand All @@ -56,7 +71,7 @@ async function handleGetRequest() {
// ToDo: (20240925 - Jacky) Implement the logic to create a new customer/vendor record in the database
async function handlePostRequest(req: NextApiRequest) {
let statusMessage: string = STATUS_MESSAGE.BAD_REQUEST;
let payload: ICustomerVendor | ICustomerVendor[] | null = null;
let payload: ICustomerVendor | null = null;

const { name, taxId, type, note } = req.body;
// ToDo: (20240925 - Jacky) Validate the request body
Expand Down Expand Up @@ -101,18 +116,21 @@ const methodHandlers: {
[key: string]: (
req: NextApiRequest,
res: NextApiResponse
) => Promise<{ statusMessage: string; payload: ICustomerVendor | ICustomerVendor[] | null }>;
) => Promise<{
statusMessage: string;
payload: ICustomerVendor | IPaginatedData<ICustomerVendor[]> | null;
}>;
} = {
GET: handleGetRequest,
POST: handlePostRequest,
};

export default async function handler(
req: NextApiRequest,
res: NextApiResponse<IResponseData<ICustomerVendor | ICustomerVendor[] | null>>
res: NextApiResponse<IResponseData<ICustomerVendor | IPaginatedData<ICustomerVendor[]> | null>>
) {
let statusMessage: string = STATUS_MESSAGE.BAD_REQUEST;
let payload: ICustomerVendor | ICustomerVendor[] | null = null;
let payload: ICustomerVendor | IPaginatedData<ICustomerVendor[]> | null = null;

try {
const handleRequest = methodHandlers[req.method || ''];
Expand All @@ -126,10 +144,9 @@ export default async function handler(
statusMessage = error.message;
payload = null;
} finally {
const { httpCode, result } = formatApiResponse<ICustomerVendor | ICustomerVendor[] | null>(
statusMessage,
payload
);
const { httpCode, result } = formatApiResponse<
ICustomerVendor | IPaginatedData<ICustomerVendor[]> | null
>(statusMessage, payload);
res.status(httpCode).json(result);
}
}
Loading

0 comments on commit d3862bb

Please sign in to comment.