diff --git a/apps/api/src/app/groups/groups.service.test.ts b/apps/api/src/app/groups/groups.service.test.ts index 812097ff..05249b02 100644 --- a/apps/api/src/app/groups/groups.service.test.ts +++ b/apps/api/src/app/groups/groups.service.test.ts @@ -46,6 +46,8 @@ describe("GroupsService", () => { groupsService = await module.resolve(GroupsService) invitesService = await module.resolve(InvitesService) + await groupsService.initialize() + const { id } = await groupsService.createGroup( { name: "Group1", @@ -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 6ccd3dfa..c36de814 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,