Skip to content

Commit

Permalink
Merge pull request #2080 from CAFECA-IO/fix/report_get_api
Browse files Browse the repository at this point in the history
fix: report formatter fixed
  • Loading branch information
Luphia authored Aug 15, 2024
2 parents 5cbec2e + e0e801b commit 571038f
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 22 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.0+8",
"version": "0.8.0+9",
"private": false,
"scripts": {
"dev": "next dev",
Expand Down
12 changes: 2 additions & 10 deletions src/lib/utils/formatter/report.formatter.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { ReportSheetType, ReportType } from '@/constants/report';
import { IPaginatedReport, IReport, IReportIncludeCompanyProject } from '@/interfaces/report';
import { isReportSheetType, isReportType } from '@/lib/utils/type_guard/report';
import { IAccountReadyForFrontend } from '@/interfaces/accounting_account';
import { isIAccountReadyForFrontendArray } from '@/lib/utils/type_guard/account';

export function formatIReport(report: IReportIncludeCompanyProject): IReport {
const type: ReportType = isReportType(report.reportType)
Expand All @@ -12,14 +10,8 @@ export function formatIReport(report: IReportIncludeCompanyProject): IReport {
? report.reportType
: ReportSheetType.BALANCE_SHEET;
const reportContent = JSON.parse(report.content as string);

// Info: (20240729 - Murky) Bad code, not robust
const content: IAccountReadyForFrontend[] = isIAccountReadyForFrontendArray(reportContent.content)
? reportContent.content
: [];

const { content, otherInfo, ...rest } = reportContent;
// Info: (20240729 - Murky) Bad code, not robust
const otherInfo = reportContent.otherInfo || {};
const project = report.project
? {
id: report.project.id.toString(),
Expand Down Expand Up @@ -47,7 +39,7 @@ export function formatIReport(report: IReportIncludeCompanyProject): IReport {
downloadLink: report.downloadLink || '',
blockChainExplorerLink: report.blockChainExplorerLink || '',
evidenceId: report.evidenceId || '',
content,
content: content ?? rest,
otherInfo,
createdAt: report.createdAt,
updatedAt: report.updatedAt,
Expand Down
3 changes: 1 addition & 2 deletions src/lib/utils/repo/report.repo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,13 @@ export async function findFirstReportByFromTo(
return report;
}

export async function findUniqueReportById(companyId: number, reportId: number) {
export async function findUniqueReportById(reportId: number) {
let report: IReportIncludeCompanyProject | null = null;

try {
report = await prisma.report.findUnique({
where: {
id: reportId,
companyId,
OR: [{ deletedAt: 0 }, { deletedAt: null }],
},
include: {
Expand Down
22 changes: 13 additions & 9 deletions src/pages/api/v1/company/[companyId]/report/[reportId]/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ export function getReportTypeFromReport(report: IReport | null) {
return reportType;
}

export async function getPeriodReport(companyId: number, reportId: number) {
const curPeriodReportFromDB = await findUniqueReportById(companyId, reportId);
export async function getPeriodReport(reportId: number) {
const curPeriodReportFromDB = await findUniqueReportById(reportId);
let curPeriodReport: IReport | null = null;
let company: Company | null = null;
if (curPeriodReportFromDB) {
Expand Down Expand Up @@ -209,16 +209,17 @@ export function formatPayloadFromIReport(report: IReport, company: Company): Fin
export async function handleGETRequest(
companyId: number,
req: NextApiRequest
): Promise<FinancialReport | null> {
): Promise<FinancialReport | null | IReport> {
const { reportIdNumber } = formatGetRequestQueryParams(req);

let payload = null;

if (reportIdNumber !== null) {
const { curPeriodReport, company } = await getPeriodReport(companyId, reportIdNumber);

if (curPeriodReport && company) {
const { curPeriodReport, company } = await getPeriodReport(reportIdNumber);
if (curPeriodReport && company && curPeriodReport.reportType !== ReportSheetType.REPORT_401) {
payload = formatPayloadFromIReport(curPeriodReport, company);
} else {
payload = curPeriodReport;
}
}

Expand All @@ -227,10 +228,10 @@ export async function handleGETRequest(

export default async function handler(
req: NextApiRequest,
res: NextApiResponse<IResponseData<FinancialReport | null>>
res: NextApiResponse<IResponseData<FinancialReport | IReport | null>>
) {
let statusMessage: string = STATUS_MESSAGE.BAD_REQUEST;
let payload: FinancialReport | null = null;
let payload: FinancialReport | IReport | null = null;
try {
const session = await getSession(req, res);
const { userId, companyId } = session;
Expand Down Expand Up @@ -258,6 +259,9 @@ export default async function handler(
console.log(error);
statusMessage = error.message;
}
const { httpCode, result } = formatApiResponse<FinancialReport | null>(statusMessage, payload);
const { httpCode, result } = formatApiResponse<FinancialReport | IReport | null>(
statusMessage,
payload
);
res.status(httpCode).json(result);
}

0 comments on commit 571038f

Please sign in to comment.