Skip to content

Commit

Permalink
fix: working solution v2
Browse files Browse the repository at this point in the history
  • Loading branch information
Kacper-RF committed Nov 3, 2023
1 parent 0f3f7dc commit bed73db
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,9 @@ export class ConnectionsManagerService extends EventEmitter implements OnModuleI
})

this.registrationService.on(RegistrationEvents.FINISHED_ISSUING_CERTIFICATES_FOR_ID, payload => {
this.storageService.resolveCsrReplicatedPromise(payload.id)
if (payload.id) {
this.storageService.resolveCsrReplicatedPromise(payload.id)
}
})
}
private attachsocketServiceListeners() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,17 @@ export class RegistrationService extends EventEmitter implements OnModuleInit {
)
}

private async issueCertificates(payload: { csrs: string[]; certificates: string[]; id: string }) {
if (!this._permsData) return
private async issueCertificates(payload: { csrs: string[]; certificates: string[]; id?: string }) {
if (!this._permsData) {
if (payload.id) this.emit(RegistrationEvents.FINISHED_ISSUING_CERTIFICATES_FOR_ID, { id: payload.id })
return
}
const pendingCsrs = await extractPendingCsrs(payload)
pendingCsrs.forEach(async csr => {
await this.registerUserCertificate(csr)
})

this.emit(RegistrationEvents.FINISHED_ISSUING_CERTIFICATES_FOR_ID, { id: payload.id })
if (payload.id) this.emit(RegistrationEvents.FINISHED_ISSUING_CERTIFICATES_FOR_ID, { id: payload.id })
}

public set permsData(perms: PermsData) {
Expand Down
20 changes: 14 additions & 6 deletions packages/backend/src/nest/storage/storage.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ interface DBOptions {
replicate: boolean
}

interface CsrReplicatedPromiseValues {
promise: Promise<unknown>
resolveFunction: any
}

@Injectable()
export class StorageService extends EventEmitter {
public channels: KeyValueStore<PublicChannel>
Expand All @@ -72,7 +77,7 @@ export class StorageService extends EventEmitter {
private filesManager: IpfsFileManagerService
private peerId: PeerId | null = null
private ipfsStarted: boolean
public csrReplicatedPromiseMap: Map<number, any> = new Map()
public csrReplicatedPromiseMap: Map<number, CsrReplicatedPromiseValues> = new Map()
private csrReplicatedPromiseId: number = 0

private readonly logger = Logger(StorageService.name)
Expand Down Expand Up @@ -403,10 +408,9 @@ export class StorageService extends EventEmitter {
}

public resolveCsrReplicatedPromise(id: number) {
console.log('rsolve promsie id', id)
if (this.csrReplicatedPromiseMap.has(id)) {
const { resolveFunction } = this.csrReplicatedPromiseMap.get(id)
resolveFunction(id)
const csrReplicatedPromiseMap = this.csrReplicatedPromiseMap.get(id)
csrReplicatedPromiseMap?.resolveFunction(id)
this.csrReplicatedPromiseMap.delete(id)
} else {
console.log(`No promise with ID ${id} found.`)
Expand All @@ -425,6 +429,7 @@ export class StorageService extends EventEmitter {

this.certificatesRequests.events.on('replicated', async () => {
this.logger('REPLICATED: CSRs')

this.csrReplicatedPromiseId++

const filteredCsrs = await this.getCsrs()
Expand All @@ -434,8 +439,11 @@ export class StorageService extends EventEmitter {
this.createCsrReplicatedPromise(this.csrReplicatedPromiseId)

if (this.csrReplicatedPromiseId > 1) {
const { promise } = this.csrReplicatedPromiseMap.get(this.csrReplicatedPromiseId - 1)
await promise
const csrReplicatedPromiseMap = this.csrReplicatedPromiseMap.get(this.csrReplicatedPromiseId - 1)

if (csrReplicatedPromiseMap?.promise) {
await csrReplicatedPromiseMap.promise
}
}

this.emit(StorageEvents.REPLICATED_CSR, {
Expand Down

0 comments on commit bed73db

Please sign in to comment.