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

add expression support for organisation instance owner name #3232

Merged
merged 21 commits into from
Apr 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
abf9bc5
deletes unused hook
cammiida Apr 1, 2025
cbc7e45
improves PartySelection error message
cammiida Apr 1, 2025
971011c
gets instance owner party from backend and correctly determines on be…
cammiida Apr 1, 2025
961904b
fixes eslint warning
cammiida Apr 1, 2025
398ae96
adds enabled prop to instance owner query, and improves useInstanceOw…
cammiida Apr 1, 2025
d0dc061
gets instance owner party from new data on instance from backend with…
cammiida Apr 3, 2025
746cd63
removes cookie utils
cammiida Apr 3, 2025
4939d8d
add expression support for organisation instance owner name
HauklandJ Apr 3, 2025
b3534ce
update tests
HauklandJ Apr 3, 2025
7a44a8b
add shared-tests
HauklandJ Apr 4, 2025
1c3ad57
expose instace owner name always
HauklandJ Apr 4, 2025
d2abb91
fixes flattening parties and checking if same party by partyId in app…
cammiida Apr 7, 2025
4aefa3a
fixes sonarqube maintainability complaint
cammiida Apr 7, 2025
26ec10a
fixes another sonarqube maintainability complaint
cammiida Apr 7, 2025
417cf15
removes unused query key
cammiida Apr 7, 2025
4a35e73
Merge branch 'fix/current-party-2' into feature/instanceOwnerName
cammiida Apr 7, 2025
2c3c1b0
gets instance owner name straight from cache instead of zustand which…
cammiida Apr 7, 2025
5efc37c
Merge branch 'main' into fix/current-party-2
cammiida Apr 7, 2025
5b97201
Merge branch 'fix/current-party-2' into feature/instanceOwnerName
cammiida Apr 7, 2025
4863aae
gets instance owner data from cache instead of performing another query
cammiida Apr 8, 2025
67180b9
Merge branch 'main' into feature/instanceOwnerName
cammiida Apr 8, 2025
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
1 change: 1 addition & 0 deletions src/features/expressions/expression-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,7 @@ export const ExprFunctionImplementations: { [K in ExprFunctionName]: Implementat
appId: true,
instanceOwnerPartyId: true,
instanceOwnerPartyType: true,
instanceOwnerName: true,
};

if (key === null || instanceDataSourcesKeys[key] !== true) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "Instance owner name when instance owner is Person",
"expression": [
"instanceContext",
"instanceOwnerName"
],
"expects": "My Org AS",
"instance": {
"id": "d00ce51c-800b-416a-a906-ccab55f597e9",
"appId": "org/app-name",
"instanceOwner": {
"organisationNumber": "1234567",
"party": {
"name": "My Org AS"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "Instance owner name when instance owner is Person",
"expression": [
"instanceContext",
"instanceOwnerName"
],
"expects": "Firstname Lastname",
"instance": {
"id": "d00ce51c-800b-416a-a906-ccab55f597e9",
"appId": "org/app-name",
"instanceOwner": {
"party": {
"name": "Firstname Lastname"
}
}
}
}
31 changes: 22 additions & 9 deletions src/features/instance/InstanceContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { Loader } from 'src/core/loading/Loader';
import { cleanUpInstanceData } from 'src/features/instance/instanceUtils';
import { ProcessProvider } from 'src/features/instance/ProcessContext';
import { useInstantiation } from 'src/features/instantiate/InstantiationContext';
import { useInstanceOwnerParty } from 'src/features/party/PartiesProvider';
import { useNavigationParam } from 'src/features/routing/AppRoutingContext';
import { buildInstanceDataSources } from 'src/utils/instanceDataSources';
import type { QueryDefinition } from 'src/core/queries/usePrefetchQuery';
Expand All @@ -22,7 +23,6 @@ import type { IData, IInstance, IInstanceDataSources } from 'src/types/shared';
export interface InstanceContext {
// Data
data: IInstance | undefined;
dataSources: IInstanceDataSources | null;

// Methods/utilities
appendDataElements: (element: IData[]) => void;
Expand Down Expand Up @@ -51,14 +51,13 @@ const {
initialCreateStore: () =>
createStore<InstanceContext>((set) => ({
data: undefined,
dataSources: null,
appendDataElements: (elements) =>
set((state) => {
if (!state.data) {
throw new Error('Cannot append data element when instance data is not set');
}
const next = { ...state.data, data: [...state.data.data, ...elements] };
return { ...state, data: next, dataSources: buildInstanceDataSources(next) };
return { ...state, data: next };
}),
mutateDataElement: (elementId, mutator) =>
set((state) => {
Expand All @@ -69,22 +68,22 @@ const {
...state.data,
data: state.data.data.map((element) => (element.id === elementId ? mutator(element) : element)),
};
return { ...state, data: next, dataSources: buildInstanceDataSources(next) };
return { ...state, data: next };
}),
removeDataElement: (elementId) =>
set((state) => {
if (!state.data) {
throw new Error('Cannot remove data element when instance data is not set');
}
const next = { ...state.data, data: state.data.data.filter((element) => element.id !== elementId) };
return { ...state, data: next, dataSources: buildInstanceDataSources(next) };
return { ...state, data: next };
}),
changeData: (callback) =>
set((state) => {
const next = callback(state.data);
const clean = cleanUpInstanceData(next);
if (clean && !deepEqual(state.data, clean)) {
return { ...state, data: next, dataSources: buildInstanceDataSources(next) };
return { ...state, data: next };
}
return {};
}),
Expand All @@ -95,13 +94,21 @@ const {
set({
reFetch: async () => {
const result = await reFetch();
set((state) => ({ ...state, data: result.data, dataSources: buildInstanceDataSources(result.data) }));
set((state) => ({ ...state, data: result.data }));
return result;
},
}),
})),
});

export const instanceQueryKeys = {
instanceData: (instanceOwnerPartyId: string | undefined, instanceGuid: string | undefined) => [
'instanceData',
instanceOwnerPartyId,
instanceGuid,
],
};

// Also used for prefetching @see appPrefetcher.ts
export function useInstanceDataQueryDef(
hasResultFromInstantiation: boolean,
Expand All @@ -110,7 +117,7 @@ export function useInstanceDataQueryDef(
): QueryDefinition<IInstance> {
const { fetchInstanceData } = useAppQueries();
return {
queryKey: ['fetchInstanceData', partyId, instanceGuid],
queryKey: instanceQueryKeys.instanceData(partyId, instanceGuid),
queryFn: partyId && instanceGuid ? () => fetchInstanceData(partyId, instanceGuid) : skipToken,
enabled: !!partyId && !!instanceGuid && !hasResultFromInstantiation,
};
Expand Down Expand Up @@ -206,10 +213,16 @@ export const useLaxInstanceStatus = () => useLaxInstance((state) => state.data?.
export const useLaxAppendDataElements = () => useLaxInstance((state) => state.appendDataElements);
export const useLaxMutateDataElement = () => useLaxInstance((state) => state.mutateDataElement);
export const useLaxRemoveDataElement = () => useLaxInstance((state) => state.removeDataElement);
export const useLaxInstanceDataSources = () => useLaxInstance((state) => state.dataSources) ?? null;
export const useLaxChangeInstance = (): ChangeInstanceData | undefined => useLaxInstance((state) => state.changeData);
export const useHasInstance = () => useHasProvider();

export function useLaxInstanceDataSources(): IInstanceDataSources | null {
const instance = useLaxInstanceData((i) => i);
const instanceOwnerParty = useInstanceOwnerParty();

return buildInstanceDataSources(instance, instanceOwnerParty);
}

/** Beware that in later versions, this will re-render your component after every save, as
* the backend sends us updated instance data */
export const useLaxInstanceDataElements = (dataType: string | undefined) =>
Expand Down
13 changes: 9 additions & 4 deletions src/features/party/PartiesProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import React, { useEffect, useState } from 'react';
import { useParams } from 'react-router';
import type { PropsWithChildren } from 'react';

import { useMutation, useQuery } from '@tanstack/react-query';
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';

import { useAppMutations, useAppQueries } from 'src/core/contexts/AppQueriesProvider';
import { createContext } from 'src/core/contexts/context';
import { delayedContext } from 'src/core/contexts/delayedContext';
import { createQueryContext } from 'src/core/contexts/queryContext';
import { DisplayError } from 'src/core/errorHandling/DisplayError';
import { Loader } from 'src/core/loading/Loader';
import { useLaxInstanceData } from 'src/features/instance/InstanceContext';
import { instanceQueryKeys } from 'src/features/instance/InstanceContext';
import { NoValidPartiesError } from 'src/features/instantiate/containers/NoValidPartiesError';
import { useShouldFetchProfile } from 'src/features/profile/ProfileProvider';
import type { IParty } from 'src/types/shared';
import type { IInstance, IParty } from 'src/types/shared';
import type { HttpClientError } from 'src/utils/network/sharedNetworking';

const partyQueryKeys = {
Expand Down Expand Up @@ -197,7 +198,11 @@ export const useSetHasSelectedParty = () => useCurrentPartyCtx().setUserHasSelec

export function useInstanceOwnerParty(): IParty | null {
const parties = usePartiesAllowedToInstantiate() ?? [];
const instanceOwner = useLaxInstanceData((i) => i.instanceOwner);
const queryClient = useQueryClient();
const { instanceOwnerPartyId, instanceGuid } = useParams();
const instanceOwner = queryClient.getQueryData<IInstance>(
instanceQueryKeys.instanceData(instanceOwnerPartyId, instanceGuid),
)?.instanceOwner;

if (!instanceOwner) {
return null;
Expand Down
1 change: 1 addition & 0 deletions src/types/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ export interface IInstanceDataSources {
appId: string;
instanceOwnerPartyId: string;
instanceOwnerPartyType: InstanceOwnerPartyType;
instanceOwnerName?: string;
}

export type IActionType = 'instantiate' | 'confirm' | 'sign' | 'reject';
Expand Down
32 changes: 32 additions & 0 deletions src/utils/instanceDataSources.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ describe('instanceDataSources/instanceContext', () => {
appId,
instanceOwner: {
partyId,
party: {
name: 'Firstname Lastname',
},
},
} as IInstance;

Expand All @@ -19,6 +22,35 @@ describe('instanceDataSources/instanceContext', () => {
instanceId: instaceId,
instanceOwnerPartyId: partyId,
instanceOwnerPartyType: 'unknown',
instanceOwnerName: 'Firstname Lastname',
};
const actual = buildInstanceDataSources(mockInstance);

expect(actual).toEqual(expected);
});

it('should build a valid instance context with organisation', () => {
const partyId = '1337';
const appId = 'tdd/enapp';
const instaceId = `${partyId}/super-secret-uuid-000`;
const mockInstance: IInstance = {
id: instaceId,
appId,
instanceOwner: {
partyId,
organisationNumber: '123456789',
party: {
name: 'My Organisation AS',
},
},
} as IInstance;

const expected: IInstanceDataSources = {
appId,
instanceId: instaceId,
instanceOwnerPartyId: partyId,
instanceOwnerPartyType: 'org',
instanceOwnerName: 'My Organisation AS',
};
const actual = buildInstanceDataSources(mockInstance);

Expand Down
10 changes: 7 additions & 3 deletions src/utils/instanceDataSources.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import type { IInstance, IInstanceDataSources } from 'src/types/shared';
import type { IInstance, IInstanceDataSources, IParty } from 'src/types/shared';

export function buildInstanceDataSources(instance?: IInstance | null | undefined): IInstanceDataSources | null {
if (!instance || !instance.instanceOwner) {
export function buildInstanceDataSources(
instance: IInstance | null | undefined,
instanceOwnerParty: IParty | null | undefined = instance?.instanceOwner?.party,
): IInstanceDataSources | null {
if (!instance?.instanceOwner) {
return null;
}
const instanceOwnerPartyType = instance.instanceOwner.organisationNumber
Expand All @@ -17,5 +20,6 @@ export function buildInstanceDataSources(instance?: IInstance | null | undefined
instanceId: instance.id,
instanceOwnerPartyId: instance.instanceOwner?.partyId,
instanceOwnerPartyType,
instanceOwnerName: instanceOwnerParty?.name,
};
}