Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(auth-admin): only update/delete delegation index of the types that are being indexed. #16627

Merged
merged 6 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading