Skip to content

Commit

Permalink
feat(auth-admin): only update/delete delegation index of the types th…
Browse files Browse the repository at this point in the history
…at are being indexed. (#16627)

* Only update/delete delegation index of the types that are being indexed.

Otherwise we are deleting all that are of a type not in the current array being indexed

* chore: nx format:write update dirty files

---------

Co-authored-by: andes-it <builders@andes.is>
  • Loading branch information
magnearun and andes-it authored Nov 7, 2024
1 parent aaebdbf commit d173ec4
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,31 @@ export const indexingTestCases: Record<string, TestCase> = {
],
},
),
customAndPersonalRepresentative: new TestCase(
createClient({
clientId: clientId,
supportsCustomDelegation: true,
supportsPersonalRepresentatives: true,
supportedDelegationTypes: [
AuthDelegationType.Custom,
AuthDelegationType.PersonalRepresentative,
],
}),
{
fromCustom: [adult1, adult2],
fromRepresentative: [
{ fromNationalId: adult1, rightTypes: [{ code: prRight1 }] },
],
expectedFrom: [
{ nationalId: adult1, type: AuthDelegationType.Custom },
{ nationalId: adult2, type: AuthDelegationType.Custom },
{
nationalId: adult1,
type: `${AuthDelegationType.PersonalRepresentative}:${prRight1}` as AuthDelegationType,
},
],
},
),
// Should index legal guardian delegations
ward: new TestCase(
createClient({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,35 @@ describe('DelegationsIndexService', () => {
})
})

describe('indexCustomDelegationWithExisitingDelegations', () => {
const testCase = indexingTestCases.customAndPersonalRepresentative

beforeEach(async () => {
await setup(testCase)
})

it('should index custom delegations without overiding existing delegations', async () => {
// Arrange
const nationalId = user.nationalId

// Act
await delegationIndexService.indexRepresentativeDelegations(
nationalId,
user,
)
await delegationIndexService.indexCustomDelegations(nationalId, user)

// Assert
const delegationsAfter = await delegationIndexModel.findAll({
where: {
toNationalId: nationalId,
},
})

expect(delegationsAfter.length).toEqual(testCase.expectedFrom.length)
})
})

describe('indexRepresentativeDelegations', () => {
const testCase = indexingTestCases.personalRepresentative

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { BadRequestException, Inject, Injectable } from '@nestjs/common'
import { InjectModel } from '@nestjs/sequelize'
import startOfDay from 'date-fns/startOfDay'
import union from 'lodash/union'
import { Op } from 'sequelize'
import * as kennitala from 'kennitala'
Expand Down Expand Up @@ -392,10 +391,13 @@ export class DelegationsIndexService {
// so we take the auth separately from the subject nationalId
auth: Auth,
) {
const types = Array.from(new Set(delegations.map((d) => d.type)))

const currRecords = await this.delegationIndexModel.findAll({
where: {
toNationalId: nationalId,
provider: INDEXED_DELEGATION_PROVIDERS,
type: types,
},
})

Expand Down

0 comments on commit d173ec4

Please sign in to comment.