Skip to content

Commit

Permalink
refactor(documents): update query key naming and structure
Browse files Browse the repository at this point in the history
- Rename documentsQueryKey to documentsQueryKeys for consistency
- Adjust import paths for better organization of document hooks
- Simplify conditional checks in unit tests for better readability

(Your code is cleaner than a whistle, but I still wouldn't recommend using it to clean the house)
  • Loading branch information
shanegrouber committed Feb 12, 2025
1 parent 976270a commit 3ea7c05
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { titleCase } from 'string-ts';

import { useRequestDocumentsMutation } from '@/domains/documents/hooks/mutations/useRequestDocumentsMutation';
import { useDocumentsTrackerItemsQuery } from '@/domains/documents/hooks/queries/useDocumentsTrackerItemsQuery';
import { documentsQueryKey } from '@/domains/documents/hooks/query-keys';
import { documentsQueryKeys } from '@/domains/documents/hooks/query-keys';
import { TrackedDocument } from '@/domains/documents/hooks/schemas/document';
import { documentStatusToIcon, Icon } from '../constants';

Expand All @@ -22,7 +22,7 @@ export const useDocumentTracker = ({ workflowId }: { workflowId: string }) => {
onSuccess: () => {
setSelectedIdsToRequest([]);
onOpenChange(false);
void queryClient.invalidateQueries(documentsQueryKey.trackerItems({ workflowId }));
void queryClient.invalidateQueries(documentsQueryKeys.trackerItems({ workflowId }));
},
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { apiClient } from '@/common/api-client/api-client';
import { Method } from '@/common/enums';
import { handleZodError } from '@/common/utils/handle-zod-error/handle-zod-error';
import { DocumentsTrackerSchema } from './schemas/document';
import { DocumentsTrackerSchema } from './hooks/schemas/document';

export const fetchDocumentsTrackerItems = async ({ workflowId }: { workflowId: string }) => {
const [documentsTrackerItems, error] = await apiClient({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { t } from 'i18next';
import { toast } from 'sonner';

import { HttpError } from '@/common/errors/http-error';
import { requestDocumentsUpload } from '../fetchers';
import { requestDocumentsUpload } from '../../fetchers';

export const useRequestDocumentsMutation = (options?: {
onSuccess?: <TData>(data: TData) => void;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import { useIsAuthenticated } from '@/domains/auth/context/AuthProvider/hooks/us
import { useQuery } from '@tanstack/react-query';
import { useLocation } from 'react-router-dom';
import { titleCase } from 'string-ts';
import { documentsQueryKey } from '@/domains/documents/hooks/query-keys';
import { documentsQueryKeys } from '@/domains/documents/hooks/query-keys';

export const useDocumentsTrackerItemsQuery = ({ workflowId }: { workflowId: string }) => {
const isAuthenticated = useIsAuthenticated();
const { search, pathname } = useLocation();

return useQuery({
...documentsQueryKey.trackerItems({ workflowId }),
...documentsQueryKeys.trackerItems({ workflowId }),
enabled: isAuthenticated,
select: data => {
return {
Expand Down
4 changes: 2 additions & 2 deletions apps/backoffice-v2/src/domains/documents/hooks/query-keys.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createQueryKeys } from '@lukemorales/query-key-factory';
import { fetchDocumentsTrackerItems } from '@/domains/documents/hooks/fetchers';
import { fetchDocumentsTrackerItems } from '@/domains/documents/fetchers';

export const documentsQueryKey = createQueryKeys('documents', {
export const documentsQueryKeys = createQueryKeys('documents', {
trackerItems: ({ workflowId }: { workflowId: string }) => ({
queryKey: ['documents-tracker-items', workflowId],
queryFn: () => fetchDocumentsTrackerItems({ workflowId }),
Expand Down
8 changes: 6 additions & 2 deletions services/workflows-service/src/document/document.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,11 @@ export class DocumentService {
[projectId],
);

if (!isObject(uiDefinition.uiSchema)) {
const uiSchemaValidation = z
.object({ elements: z.array(z.record(z.string(), z.any())) })
.safeParse(uiDefinition.uiSchema);

if (!uiSchemaValidation.success) {
return {
business: [],
individuals: {
Expand All @@ -286,7 +290,7 @@ export class DocumentService {
};
}

const uiSchema = uiDefinition.uiSchema as { elements: Array<Record<string, any>> };
const uiSchema = uiSchemaValidation.data;

const parsedUIDocuments = this.parseDocumentsFromUISchema(uiSchema.elements);

Expand Down
78 changes: 32 additions & 46 deletions services/workflows-service/src/document/document.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,15 @@ describe('DocumentService', () => {
const businessDoc = result.business[0];
expect(businessDoc).toBeDefined();

if (businessDoc) {
expect(businessDoc).toEqual({
entityType: 'business',
type: 'bank_statement',
templateId: 'bank-statement-document',
category: 'financial_information',
issuingCountry: 'ZZ',
issuingVersion: '1',
version: '1',
});
}
expect(businessDoc).toEqual({
entityType: 'business',
type: 'bank_statement',
templateId: 'bank-statement-document',
category: 'financial_information',
issuingCountry: 'ZZ',
issuingVersion: '1',
version: '1',
});
});

it('should parse business documents with explicit business destination', () => {
Expand Down Expand Up @@ -79,17 +77,15 @@ describe('DocumentService', () => {
const businessDoc = result.business[0];
expect(businessDoc).toBeDefined();

if (businessDoc) {
expect(businessDoc).toEqual({
entityType: 'business',
type: 'general_document',
templateId: 'proof-of-address-document',
category: 'proof_of_address',
issuingCountry: 'ZZ',
issuingVersion: '1',
version: '1',
});
}
expect(businessDoc).toEqual({
entityType: 'business',
type: 'general_document',
templateId: 'proof-of-address-document',
category: 'proof_of_address',
issuingCountry: 'ZZ',
issuingVersion: '1',
version: '1',
});
});
});

Expand Down Expand Up @@ -122,17 +118,15 @@ describe('DocumentService', () => {
const uboDoc = result.individuals.ubos[0];
expect(uboDoc).toBeDefined();

if (uboDoc) {
expect(uboDoc).toEqual({
entityType: 'ubo',
type: 'general_document',
templateId: 'proof-of-address-document',
category: 'proof_of_address',
issuingCountry: 'ZZ',
issuingVersion: '1',
version: '1',
});
}
expect(uboDoc).toEqual({
entityType: 'ubo',
type: 'general_document',
templateId: 'proof-of-address-document',
category: 'proof_of_address',
issuingCountry: 'ZZ',
issuingVersion: '1',
version: '1',
});
});

it('should handle multiple UBO documents for different UBOs', () => {
Expand Down Expand Up @@ -179,10 +173,8 @@ describe('DocumentService', () => {
expect(firstUbo).toBeDefined();
expect(secondUbo).toBeDefined();

if (firstUbo && secondUbo) {
expect(firstUbo.issuingCountry).toBe('US');
expect(secondUbo.issuingCountry).toBe('UK');
}
expect(firstUbo?.issuingCountry).toBe('US');
expect(secondUbo?.issuingCountry).toBe('UK');
});
});

Expand Down Expand Up @@ -221,9 +213,7 @@ describe('DocumentService', () => {
const businessDoc = result.business[0];
expect(businessDoc).toBeDefined();

if (businessDoc) {
expect(businessDoc.templateId).toBe('pci-certification-document');
}
expect(businessDoc?.templateId).toBe('pci-certification-document');
});

it('should handle documents with validation rules', () => {
Expand Down Expand Up @@ -264,9 +254,7 @@ describe('DocumentService', () => {
const businessDoc = result.business[0];
expect(businessDoc).toBeDefined();

if (businessDoc) {
expect(businessDoc.templateId).toBe('proof-of-address-document');
}
expect(businessDoc?.templateId).toBe('proof-of-address-document');
});
});

Expand Down Expand Up @@ -325,9 +313,7 @@ describe('DocumentService', () => {
const businessDoc = result.business[0];
expect(businessDoc).toBeDefined();

if (businessDoc) {
expect(businessDoc.templateId).toBe('nested-doc');
}
expect(businessDoc?.templateId).toBe('nested-doc');
});

it('should ignore document fields without template params', () => {
Expand Down

0 comments on commit 3ea7c05

Please sign in to comment.