Skip to content

Commit

Permalink
Merge pull request #112 from codeforjapan/mitomo/random_ids
Browse files Browse the repository at this point in the history
🐛 Fix request body randomID to Array
  • Loading branch information
shogo-mitomo authored May 6, 2020
2 parents 313e78b + a5cbd3a commit ead939e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 15 deletions.
15 changes: 12 additions & 3 deletions src/users/dto/delete-user-organization.dto.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
import { ApiPropertyOptional } from '@nestjs/swagger'
import { IsString, IsOptional, IsNotEmpty } from 'class-validator'
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger'
import { IsString, IsOptional, IsNotEmpty, IsArray, ValidateNested } from 'class-validator'
import { Type } from 'class-transformer'

export class DeleteUserOrganizationDto {
export class RandomIDDto {
@ApiPropertyOptional()
@IsOptional()
@IsString()
@IsNotEmpty()
randomID: string
}

export class DeleteUserOrganizationDto {
@ApiProperty({ type: RandomIDDto, isArray: true })
@IsArray()
@ValidateNested({ each: true })
@Type(() => RandomIDDto)
randomIDs: RandomIDDto[]

// Keys without any decorators are non-Whitelisted. Validator will throw error if it's passed in payload.
userId: string
Expand Down
28 changes: 16 additions & 12 deletions src/users/users.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,23 +98,27 @@ export class UsersRepository {
async deleteDiagnosisKeysForOrg(
deleteUserOrganization: DeleteUserOrganizationDto
): Promise<void> {
const { organizationCode, randomID } = deleteUserOrganization
const { organizationCode, randomIDs } = deleteUserOrganization

if (!randomID) {
if (randomIDs.length === 0) {
return
}

await (await this.firestoreDB)
.collection('diagnosisKeysForOrg')
.doc(organizationCode)
.collection('tempIDs')
.where('randomID', '==', randomID)
.get()
.then((query) => {
query.forEach((doc) => {
doc.ref.delete()
})
await Promise.all(
randomIDs.map(async ({ randomID }) => {
await (await this.firestoreDB)
.collection('diagnosisKeysForOrg')
.doc(organizationCode)
.collection('tempIDs')
.where('randomID', '==', randomID)
.get()
.then((query) => {
query.forEach((doc) => {
doc.ref.delete()
})
})
})
)
}

async createDiagnosisKeysForOrg(
Expand Down

0 comments on commit ead939e

Please sign in to comment.