35 lines
939 B
TypeScript
35 lines
939 B
TypeScript
import type { Document, DocumentType } from './types';
|
|
|
|
export function combineDocuments({
|
|
documentTypes,
|
|
documents,
|
|
}: {
|
|
documentTypes: DocumentType[];
|
|
documents: Document[];
|
|
}) {
|
|
if (!documents.length) {
|
|
return documentTypes.map((x) => ({ ...(x as Document), canUpload: true }));
|
|
}
|
|
|
|
const nonUploadableDocuments = documents
|
|
.filter(
|
|
(document) =>
|
|
!documentTypes.some(
|
|
(documentType) => documentType.documentTypeId === document.documentTypeId
|
|
)
|
|
)
|
|
.map((document) => ({ ...document, canUpload: false }));
|
|
|
|
return documentTypes
|
|
.map((documentType) => {
|
|
const targetDocument = documents.find(
|
|
(document) => document.documentTypeId === documentType.documentTypeId
|
|
);
|
|
|
|
return { ...documentType, ...targetDocument, canUpload: true };
|
|
})
|
|
.concat(nonUploadableDocuments);
|
|
}
|
|
|
|
export type CombinedDocuments = ReturnType<typeof combineDocuments>;
|