Skip to content

Commit

Permalink
Merge pull request #330 from privacy-scaling-explorations/fix/cached-…
Browse files Browse the repository at this point in the history
…groups

Wrong initialization of cached groups in Group service
  • Loading branch information
vplasencia authored Nov 9, 2023
2 parents 2e36b6d + 56b3177 commit e9bd9da
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
15 changes: 15 additions & 0 deletions apps/api/src/app/groups/groups.service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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)
})
})
})
12 changes: 9 additions & 3 deletions apps/api/src/app/groups/groups.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
4 changes: 4 additions & 0 deletions apps/api/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit e9bd9da

Please sign in to comment.