Skip to content

Commit

Permalink
Merge pull request #238 from tnpfldyd/BE-fix/server-api
Browse files Browse the repository at this point in the history
๋ฒ„๊ทธ ํ”ฝ์Šค ๋ฐ ๋ถˆํ•„์š” ์ฝ”๋“œ ์‚ญ์ œ
  • Loading branch information
Conut-1 authored Dec 11, 2023
2 parents 3d501c2 + 2cece3c commit a0b003b
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 50 deletions.
10 changes: 9 additions & 1 deletion .github/workflows/BE-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ jobs:
echo "STORAGE_URL=$STORAGE_URL" >> ./nestjs-BE/server/.env
echo "BASE_IMAGE_URL=$BASE_IMAGE_URL" >> ./nestjs-BE/server/.env
echo "BUCKET_NAME=$BUCKET_NAME" >> ./nestjs-BE/server/.env
echo "APP_ICON_URL=$APP_ICON_URL" >> ./nestjs-BE/server/.env
echo "CSV_FOLDER=$CSV_FOLDER" >> ./nestjs-BE/server/.env
docker build -t ghcr.io/${{ secrets.PACKAGE_USERNAME }}/mindsync ./nestjs-BE/server
docker push ghcr.io/${{ secrets.PACKAGE_USERNAME }}/mindsync:latest
env:
Expand All @@ -44,6 +46,8 @@ jobs:
STORAGE_URL: ${{ secrets.STORAGE_URL }}
BASE_IMAGE_URL: ${{ secrets.BASE_IMAGE_URL }}
BUCKET_NAME: ${{ secrets.BUCKET_NAME }}
APP_ICON_URL: ${{ secrets.APP_ICON_URL }}
CSV_FOLDER: ${{ secrets.CSV_FOLDER }}

deploy:
needs: build
Expand All @@ -61,4 +65,8 @@ jobs:
docker pull ghcr.io/${{ secrets.PACKAGE_USERNAME }}/mindsync
docker stop mindsync_server || true
docker rm mindsync_server || true
docker run -d --name mindsync_server -p ${{ secrets.SERVER_PORT }}:${{ secrets.CONTAINER_PORT }} ghcr.io/${{ secrets.PACKAGE_USERNAME }}/mindsync
docker run -d \
--name mindsync_server \
-p ${{ secrets.SERVER_PORT }}:${{ secrets.CONTAINER_PORT }} \
-v temporary-volume:${{ secrets.CSV_FOLDER }} \
ghcr.io/${{ secrets.PACKAGE_USERNAME }}/mindsync
2 changes: 0 additions & 2 deletions nestjs-BE/server/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ RUN npx prisma generate --schema=./prisma/mysql.schema.prisma

RUN npx prisma generate --schema=./prisma/mongodb.schema.prisma

RUN mkdir operations

EXPOSE 3000

CMD ["npm", "start"]
4 changes: 2 additions & 2 deletions nestjs-BE/server/src/profile-space/profile-space.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export class ProfileSpaceService extends BaseService<UpdateProfileSpaceDto> {
},
});
const storeUserSpaces =
profileResponse.spaces.map((profileSpace) => profileSpace.space) || [];
profileResponse?.spaces.map((profileSpace) => profileSpace.space) || [];
return storeUserSpaces;
}

Expand Down Expand Up @@ -147,7 +147,7 @@ export class ProfileSpaceService extends BaseService<UpdateProfileSpaceDto> {
const users = await this.getSpaceUsers(spaceUuid);
const usersData = await Promise.all(
users.map(async (user) => {
return await this.profilesService.getDataFromCacheOrDB(user.user_id);
return await this.profilesService.findOne(user.user_id);
}),
);
this.spaceCache.put(spaceUuid, usersData);
Expand Down
4 changes: 2 additions & 2 deletions nestjs-BE/server/src/spaces/spaces.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { UploadService } from 'src/upload/upload.service';
import { ProfileSpaceService } from 'src/profile-space/profile-space.service';
import { RequestWithUser } from 'src/utils/interface';
import customEnv from 'src/config/env';
const { BASE_IMAGE_URL } = customEnv;
const { APP_ICON_URL } = customEnv;

@Controller('spaces')
@ApiTags('spaces')
Expand All @@ -43,7 +43,7 @@ export class SpacesController {
) {
const iconUrl = icon
? await this.uploadService.uploadFile(icon)
: BASE_IMAGE_URL;
: APP_ICON_URL;
createSpaceDto.icon = iconUrl;
const response = await this.spacesService.create(createSpaceDto);
const { uuid: spaceUuid } = response.data;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,57 @@ import {
import { Cron } from '@nestjs/schedule';
import { promises as fs } from 'fs';
import { join } from 'path';
import { TokenData } from 'src/auth/auth.service';
import { InviteCodeData } from 'src/invite-codes/invite-codes.service';
import { CreateProfileSpaceDto } from 'src/profile-space/dto/create-profile-space.dto';
import { UpdateProfileDto } from 'src/profiles/dto/update-profile.dto';
import { UpdateSpaceDto } from 'src/spaces/dto/update-space.dto';
import { UpdateUserDto } from 'src/users/dto/update-user.dto';
import costomEnv from 'src/config/env';
const { CSV_FOLDER } = costomEnv;

type DeleteDataType = {
field: string;
value: string;
};

type InsertDataType =
| TokenData
| InviteCodeData
| CreateProfileSpaceDto
| UpdateProfileDto
| UpdateSpaceDto
| UpdateUserDto;

type UpdateDataType = {
field: string;
value: InsertDataType;
};
type DataType = InsertDataType | UpdateDataType | DeleteDataType;

interface OperationData {
service: string;
uniqueKey: string;
command: string;
data: any;
data: DataType;
}

@Injectable()
export class TemporaryDatabaseService {
private database: Map<string, Map<string, Map<string, any>>> = new Map();
private entriesMap: Map<string, string[]> = new Map();
private readonly FOLDER_NAME = 'operations';
private database: Map<string, Map<string, Map<string, DataType>>> = new Map();
private readonly FOLDER_NAME = CSV_FOLDER;

constructor(
private readonly prismaMysql: PrismaServiceMySQL,
private readonly prismaMongoDB: PrismaServiceMongoDB,
) {
this.init();
}

async init() {
this.initializeDatabase();
this.readDataFromFiles();
this.executeBulkOperations();
await this.readDataFromFiles();
await this.executeBulkOperations();
}

private initializeDatabase() {
Expand All @@ -52,11 +82,11 @@ export class TemporaryDatabaseService {

private async readDataFromFiles() {
const files = await fs.readdir(this.FOLDER_NAME);
files.forEach((file) => {
if (file.endsWith('.csv')) {
this.readDataFromFile(file);
}
});
return Promise.all(
files
.filter((file) => file.endsWith('.csv'))
.map((file) => this.readDataFromFile(file)),
);
}

private async readDataFromFile(file: string) {
Expand All @@ -79,15 +109,15 @@ export class TemporaryDatabaseService {
return this.database.get(service).get(command).get(uniqueKey);
}

create(service: string, uniqueKey: string, data: any) {
create(service: string, uniqueKey: string, data: InsertDataType) {
this.operation({ service, uniqueKey, command: 'insert', data });
}

update(service: string, uniqueKey: string, data: any) {
update(service: string, uniqueKey: string, data: UpdateDataType) {
this.operation({ service, uniqueKey, command: 'update', data });
}

remove(service: string, uniqueKey: string, data: any) {
remove(service: string, uniqueKey: string, data: DeleteDataType) {
this.operation({ service, uniqueKey, command: 'delete', data });
}

Expand All @@ -105,14 +135,10 @@ export class TemporaryDatabaseService {

operation({ service, uniqueKey, command, data }: OperationData) {
const filePath = join(this.FOLDER_NAME, `${service}-${command}.csv`);
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);
});
fs.appendFile(filePath, `${uniqueKey},${JSON.stringify(data)}\n`, 'utf8');
}

@Cron('* * * * * *')
@Cron('0 */10 * * * *')
async executeBulkOperations() {
for (const service of this.database.keys()) {
const serviceMap = this.database.get(service);
Expand All @@ -126,11 +152,10 @@ export class TemporaryDatabaseService {

private async performInsert(
service: string,
dataMap: Map<string, any>,
dataMap: Map<string, DataType>,
prisma: PrismaServiceMongoDB | PrismaServiceMySQL,
) {
const data = this.prepareData(service, 'insert', dataMap);
this.entriesMap.clear();
if (!data.length) return;
if (prisma instanceof PrismaServiceMySQL) {
await prisma[service].createMany({
Expand All @@ -146,7 +171,7 @@ export class TemporaryDatabaseService {

private async performUpdate(
service: string,
dataMap: Map<string, any>,
dataMap: Map<string, DataType>,
prisma: PrismaServiceMongoDB | PrismaServiceMySQL,
) {
const data = this.prepareData(service, 'update', dataMap);
Expand All @@ -168,7 +193,7 @@ export class TemporaryDatabaseService {

private async performDelete(
service: string,
dataMap: Map<string, any>,
dataMap: Map<string, DataType>,
prisma: PrismaServiceMongoDB | PrismaServiceMySQL,
) {
const data = this.prepareData(service, 'delete', dataMap);
Expand Down Expand Up @@ -200,23 +225,4 @@ export class TemporaryDatabaseService {
private clearFile(filename: string) {
fs.writeFile(join(this.FOLDER_NAME, filename), '', 'utf8');
}

getEntries(key: string): string[] {
return this.entriesMap.get(key) || [];
}

addEntry(key: string, value: string): void {
const entries = this.getEntries(key);
entries.push(value);
this.entriesMap.set(key, entries);
}

removeEntry(key: string, value: string): void {
const entries = this.getEntries(key);
const index = entries.indexOf(value);
if (index > -1) {
entries.splice(index, 1);
this.entriesMap.set(key, entries);
}
}
}

0 comments on commit a0b003b

Please sign in to comment.