Skip to content

Commit

Permalink
fix(#58): 파일 읽고 쓰기 비동기 처리 및 일부 any 삭제
Browse files Browse the repository at this point in the history
  • Loading branch information
tnpfldyd committed Nov 27, 2023
1 parent 28e9529 commit 9520327
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 13 deletions.
2 changes: 1 addition & 1 deletion nestjs-BE/cache-server/src/profiles/profiles.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class ProfilesService extends BaseService<UpdateProfileDto> {
});
}

generateKey(data: any): string {
generateKey(data: UpdateProfileDto): string {
return data.uuid;
}

Expand Down
2 changes: 1 addition & 1 deletion nestjs-BE/cache-server/src/spaces/spaces.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class SpacesService extends BaseService<UpdateSpaceDto> {
});
}

generateKey(data: any): string {
generateKey(data: UpdateSpaceDto): string {
return data.uuid;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Injectable } from '@nestjs/common';
import { PrismaService } from '../prisma/prisma.service';
import { Cron } from '@nestjs/schedule';
import { writeFileSync, readFileSync, existsSync, readdirSync } from 'fs';
import { promises as fs } from 'fs';
import { join } from 'path';

interface OperationData {
Expand Down Expand Up @@ -35,19 +35,19 @@ export class TemporaryDatabaseService {
});
}

private readDataFromFiles() {
const files = readdirSync(this.FOLDER_NAME);
private async readDataFromFiles() {
const files = await fs.readdir(this.FOLDER_NAME);
files.forEach((file) => {
if (file.endsWith('.csv')) {
this.readDataFromFile(file);
}
});
}

private readDataFromFile(file: string) {
private async readDataFromFile(file: string) {
const [service, commandWithExtension] = file.split('-');
const command = commandWithExtension.replace('.csv', '');
const fileData = readFileSync(join(this.FOLDER_NAME, file), 'utf8');
const fileData = await fs.readFile(join(this.FOLDER_NAME, file), 'utf8');
fileData.split('\n').forEach((line) => {
if (line.trim() !== '') {
const [uniqueKey, ...dataParts] = line.split(',');
Expand Down Expand Up @@ -82,10 +82,11 @@ export class TemporaryDatabaseService {

operation({ service, uniqueKey, command, data }: OperationData) {
const filePath = join(this.FOLDER_NAME, `${service}-${command}.csv`);
let fileData = existsSync(filePath) ? readFileSync(filePath, 'utf8') : '';
fileData += `${uniqueKey},${JSON.stringify(data)}\n`;
writeFileSync(filePath, fileData);
this.database.get(service).get(command).set(uniqueKey, data);
fs.readFile(filePath, 'utf8').then((fileData) => {
fileData += `${uniqueKey},${JSON.stringify(data)}\n`;
fs.writeFile(filePath, fileData);
this.database.get(service).get(command).set(uniqueKey, data);
});
}

@Cron('0 */10 * * * *')
Expand Down Expand Up @@ -151,7 +152,7 @@ export class TemporaryDatabaseService {
}

private clearFile(filename: string) {
writeFileSync(join(this.FOLDER_NAME, filename), '', 'utf8');
fs.writeFile(join(this.FOLDER_NAME, filename), '', 'utf8');
}

getUserProfiles(user_id: string): string[] {
Expand Down
2 changes: 1 addition & 1 deletion nestjs-BE/cache-server/src/users/users.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class UsersService extends BaseService<UpdateUserDto> {
});
}

generateKey(data: any): string {
generateKey(data: UpdateUserDto): string {
return data.email;
}

Expand Down

0 comments on commit 9520327

Please sign in to comment.