From 56b3177e77a92ef0b91df50688a7725a7cfcfea9 Mon Sep 17 00:00:00 2001 From: Jeeiii Date: Thu, 9 Nov 2023 16:15:45 +0100 Subject: [PATCH] fix: wrong initialization of cached groups in Group service --- apps/api/src/app/groups/groups.service.test.ts | 15 +++++++++++++++ apps/api/src/app/groups/groups.service.ts | 12 +++++++++--- apps/api/src/main.ts | 4 ++++ 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/apps/api/src/app/groups/groups.service.test.ts b/apps/api/src/app/groups/groups.service.test.ts index 812097ff..3dbb4e76 100644 --- a/apps/api/src/app/groups/groups.service.test.ts +++ b/apps/api/src/app/groups/groups.service.test.ts @@ -44,6 +44,8 @@ describe("GroupsService", () => { }).compile() groupsService = await module.resolve(GroupsService) + await groupsService.initialize() + invitesService = await module.resolve(InvitesService) const { id } = await groupsService.createGroup( @@ -723,4 +725,17 @@ describe("GroupsService", () => { await expect(fun).rejects.toThrow("You are not the admin") }) }) + + describe("# initialize", () => { + it("Should initialize the cached groups", async () => { + const currentCachedGroups = await groupsService.getGroups() + + await groupsService.initialize() + + const updatedCachedGroups = await groupsService.getGroups() + + expect(currentCachedGroups).toHaveLength(updatedCachedGroups.length) + expect(currentCachedGroups).toStrictEqual(updatedCachedGroups) + }) + }) }) diff --git a/apps/api/src/app/groups/groups.service.ts b/apps/api/src/app/groups/groups.service.ts index 42836a9c..edf03ccf 100644 --- a/apps/api/src/app/groups/groups.service.ts +++ b/apps/api/src/app/groups/groups.service.ts @@ -39,13 +39,19 @@ export class GroupsService { process.env.BACKEND_PRIVATE_KEY as string, process.env.INFURA_API_KEY as string ) + } - this._cacheGroups() + /** + * Initialises the service, caches groups and may sync contract + * groups if required. + */ + async initialize() { + await this._cacheGroups() /* istanbul ignore next */ if (process.env.NODE_ENV !== "test") { - setTimeout(() => { - this._syncContractGroups() + setTimeout(async () => { + await this._syncContractGroups() }, 5000) } } diff --git a/apps/api/src/main.ts b/apps/api/src/main.ts index ea97a6c8..cbd02fe5 100644 --- a/apps/api/src/main.ts +++ b/apps/api/src/main.ts @@ -3,10 +3,14 @@ import { NestFactory } from "@nestjs/core" import { SwaggerModule, DocumentBuilder } from "@nestjs/swagger" import { ironSession } from "iron-session/express" import { AppModule } from "./app/app.module" +import { GroupsService } from "./app/groups/groups.service" async function bootstrap() { const app = await NestFactory.create(AppModule) + const groupService = app.get(GroupsService) + await groupService.initialize() + app.useGlobalPipes( new ValidationPipe({ whitelist: true,