Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions apps/backend/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,10 @@ export enum FileNames {
TDI34 = 'F08TDI34',
SBC_SALES = 'SBC_SALES'
}

export interface ParseArgsTDI {
type: FileTypes;
fileName: string;
program: string;
fileContents: string;
}
15 changes: 8 additions & 7 deletions apps/backend/src/lambdas/parseFlatFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import { NestFactory } from '@nestjs/core';
import { Context } from 'aws-lambda';
import { parseTDI } from './utils/parseTDI';
import { AppModule } from '../app.module';
import { FileTypes } from '../constants';
import { AppLogger } from '../logger/logger.service';
import { S3ManagerService } from '../s3-manager/s3-manager.service';

export interface parseFlatFileEvent {
type: string;
type: FileTypes;
program: string;
filepath: string;
filename: string;
Expand Down Expand Up @@ -36,12 +37,12 @@ export const handler = async (event: parseFlatFileEvent, context?: Context) => {
await uploadParsedTDI(
event.type,
s3manager,
parseTDI(
event.type,
contents?.Body?.toString() || '',
event.program,
event.filename
),
parseTDI({
type: event.type,
fileContents: contents?.Body?.toString() || '',
program: event.program,
fileName: event.filename
}),
appLogger,
event?.outputPath ?? undefined
);
Expand Down
15 changes: 7 additions & 8 deletions apps/backend/src/lambdas/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import { TransactionEntity } from '../transaction/entities/transaction.entity';
import { SBCGarmsJson } from '../transaction/interface';
import { PaymentMethodService } from '../transaction/payment-method.service';
import { TransactionService } from '../transaction/transaction.service';

export interface ParseEvent {
eventType: string;
filename: string;
Expand Down Expand Up @@ -107,7 +106,7 @@ export const handler = async (event?: unknown, _context?: Context) => {
if (filename.includes(FileNames.TDI17)) return FileTypes.TDI17;
if (filename.includes(FileNames.TDI34)) return FileTypes.TDI34;
if (filename.includes(FileNames.SBC_SALES)) return FileTypes.SBC_SALES;
throw new Error('Unknow file type: ' + filename);
throw new Error('Unknown file type: ' + filename);
})();

const ministry = (() => {
Expand All @@ -133,12 +132,12 @@ export const handler = async (event?: unknown, _context?: Context) => {
}

if (fileType === FileTypes.TDI17 || fileType === FileTypes.TDI34) {
const parsed = parseTDI(
fileType,
Buffer.from(file.Body?.toString() || '').toString(),
ministry,
filename
);
const parsed = parseTDI({
type: fileType,
fileName: filename,
program: ministry,
fileContents: Buffer.from(file.Body?.toString() || '').toString()
});

if (fileType === FileTypes.TDI34) {
const tdi34Details = parsed as TDI34Details[];
Expand Down
16 changes: 8 additions & 8 deletions apps/backend/src/lambdas/utils/parseTDI.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { FileTypes } from '../../constants';
import { ParseArgsTDI, FileTypes } from '../../constants';
import { TDI17Details, TDI34Details, DDFDetails } from '../../flat-files';

export const parseTDI = (
type: string,
program: string,
fileName: string,
fileContents: string
): TDI34Details[] | TDI17Details[] | DDFDetails[] | [] => {
const lines = fileContents?.split('\n').filter((l: string) => l);
export const parseTDI = ({
type,
fileName,
program,
fileContents
}: ParseArgsTDI): TDI34Details[] | TDI17Details[] | DDFDetails[] | [] => {
const lines = fileContents.split('\n').filter((l: string) => l);
lines.splice(0, 1);
lines.splice(lines.length - 1, 1);

Expand Down
16 changes: 9 additions & 7 deletions apps/backend/src/parse/parse.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { FileInterceptor } from '@nestjs/platform-express';
import { ApiConsumes, ApiBody, ApiTags } from '@nestjs/swagger';
import { ParseService } from './parse.service';
import { AppLogger } from '../logger/logger.service';
import { FileTypes } from './../constants';

@Controller('parse')
@ApiTags('Parser API')
Expand Down Expand Up @@ -49,14 +50,15 @@ export class ParseController {
})
@UseInterceptors(FileInterceptor('file'))
uploadFile(
@Body() body: { program: string; fileType: string },
@Body() body: { program: string; fileType: FileTypes },
@UploadedFile() file: Express.Multer.File
) {
return this.parseService.readAndParseFile(
body.fileType,
body.program,
file.originalname,
file.buffer
);
const contents = file.buffer.toString();
return this.parseService.readAndParseFile({
type: body.fileType,
fileName: file.originalname,
program: body.program,
fileContents: contents
});
}
}
15 changes: 8 additions & 7 deletions apps/backend/src/parse/parse.service.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import { Inject, Injectable, Logger } from '@nestjs/common';
import { ParseArgsTDI } from '../constants';
import { parseTDI } from '../lambdas/utils/parseTDI';
import { AppLogger } from '../logger/logger.service';

@Injectable()
export class ParseService {
constructor(@Inject(Logger) private readonly appLogger: AppLogger) {}

async readAndParseFile(
type: string,
program: string,
fileName: string,
data: Buffer
): Promise<unknown> {
async readAndParseFile({
type,
fileName,
program,
fileContents
}: ParseArgsTDI): Promise<unknown> {
try {
return parseTDI(type, program, fileName, data.toString());
return parseTDI({ type, fileName, program, fileContents });
} catch (err) {
this.appLogger.error(err, 'Error parsing file');
throw err;
Expand Down