This repository has no description
0

Configure Feed

Select the types of activity you want to include in your feed.

semble / src / modules / cards / infrastructure / repositories / mappers / CollectionMapper.ts
7.2 kB 230 lines
1import { UniqueEntityID } from '../../../../../shared/domain/UniqueEntityID'; 2import { Collection, CollectionAccessType } from '../../../domain/Collection'; 3import { CollectionId } from '../../../domain/value-objects/CollectionId'; 4import { CardId } from '../../../domain/value-objects/CardId'; 5import { CuratorId } from '../../../domain/value-objects/CuratorId'; 6import { PublishedRecordId } from '../../../domain/value-objects/PublishedRecordId'; 7import { PublishedRecordDTO, PublishedRecordRefDTO } from './DTOTypes'; 8import { CollectionQueryResultDTO } from '../../../domain/ICollectionQueryRepository'; 9import { err, ok, Result } from '../../../../../shared/core/Result'; 10 11// Database representation of a collection 12export interface CollectionDTO extends PublishedRecordRefDTO { 13 id: string; 14 authorId: string; 15 name: string; 16 description?: string; 17 accessType: string; 18 cardCount: number; 19 createdAt: Date; 20 updatedAt: Date; 21 collaborators?: string[]; 22 cardLinks?: { 23 cardId: string; 24 addedBy: string; 25 addedAt: Date; 26 publishedRecordId?: string; 27 publishedRecord?: PublishedRecordDTO; 28 }[]; 29} 30 31export class CollectionMapper { 32 public static toQueryResult(raw: { 33 id: string; 34 uri: string | null; 35 name: string; 36 description?: string | null; 37 accessType: string; 38 createdAt: Date; 39 updatedAt: Date; 40 authorId: string; 41 cardCount: number; 42 }): CollectionQueryResultDTO { 43 return { 44 id: raw.id, 45 uri: raw.uri || undefined, 46 name: raw.name, 47 description: raw.description || undefined, 48 accessType: raw.accessType, 49 createdAt: raw.createdAt, 50 updatedAt: raw.updatedAt, 51 authorId: raw.authorId, 52 cardCount: raw.cardCount, 53 }; 54 } 55 56 public static toDomain(dto: CollectionDTO): Result<Collection> { 57 try { 58 // Create value objects 59 const authorIdOrError = CuratorId.create(dto.authorId); 60 if (authorIdOrError.isErr()) return err(authorIdOrError.error); 61 62 // Create collaborator IDs 63 const collaboratorIds: CuratorId[] = []; 64 if (dto.collaborators) { 65 for (const collaboratorId of dto.collaborators) { 66 const curatorIdOrError = CuratorId.create(collaboratorId); 67 if (curatorIdOrError.isErr()) return err(curatorIdOrError.error); 68 collaboratorIds.push(curatorIdOrError.value); 69 } 70 } 71 72 // Create card links 73 const cardLinks: any[] = []; 74 if (dto.cardLinks) { 75 for (const linkDto of dto.cardLinks) { 76 const cardIdOrError = CardId.createFromString(linkDto.cardId); 77 if (cardIdOrError.isErr()) return err(cardIdOrError.error); 78 79 const addedByOrError = CuratorId.create(linkDto.addedBy); 80 if (addedByOrError.isErr()) return err(addedByOrError.error); 81 82 let publishedRecordId: PublishedRecordId | undefined; 83 if (linkDto.publishedRecord) { 84 publishedRecordId = PublishedRecordId.create({ 85 uri: linkDto.publishedRecord.uri, 86 cid: linkDto.publishedRecord.cid, 87 }); 88 } 89 90 cardLinks.push({ 91 cardId: cardIdOrError.value, 92 addedBy: addedByOrError.value, 93 addedAt: linkDto.addedAt, 94 publishedRecordId, 95 }); 96 } 97 } 98 99 // Create optional published record ID 100 let publishedRecordId: PublishedRecordId | undefined; 101 if (dto.publishedRecord) { 102 publishedRecordId = PublishedRecordId.create({ 103 uri: dto.publishedRecord.uri, 104 cid: dto.publishedRecord.cid, 105 }); 106 } 107 108 // Create the collection 109 const collectionOrError = Collection.create( 110 { 111 authorId: authorIdOrError.value, 112 name: dto.name, 113 description: dto.description, 114 accessType: dto.accessType as CollectionAccessType, 115 collaboratorIds, 116 cardLinks, 117 cardCount: dto.cardCount, 118 publishedRecordId, 119 createdAt: dto.createdAt, 120 updatedAt: dto.updatedAt, 121 }, 122 new UniqueEntityID(dto.id), 123 ); 124 125 if (collectionOrError.isErr()) return err(collectionOrError.error); 126 127 const collection = collectionOrError.value; 128 129 return ok(collection); 130 } catch (error) { 131 return err(error as Error); 132 } 133 } 134 135 public static toPersistence(collection: Collection): { 136 collection: { 137 id: string; 138 authorId: string; 139 name: string; 140 description?: string; 141 accessType: string; 142 createdAt: Date; 143 updatedAt: Date; 144 cardCount: number; 145 publishedRecordId?: string; 146 }; 147 collaborators: { 148 id: string; 149 collectionId: string; 150 collaboratorId: string; 151 }[]; 152 cardLinks: { 153 id: string; 154 collectionId: string; 155 cardId: string; 156 addedBy: string; 157 addedAt: Date; 158 publishedRecordId?: string; 159 }[]; 160 publishedRecord?: PublishedRecordDTO; 161 linkPublishedRecords?: PublishedRecordDTO[]; 162 } { 163 // Create published record data if it exists 164 let publishedRecord: PublishedRecordDTO | undefined; 165 let publishedRecordId: string | undefined; 166 167 if (collection.publishedRecordId) { 168 const recordId = new UniqueEntityID().toString(); 169 publishedRecord = { 170 id: recordId, 171 uri: collection.publishedRecordId.uri, 172 cid: collection.publishedRecordId.cid, 173 recordedAt: new Date(), 174 }; 175 publishedRecordId = recordId; 176 } 177 178 // Create collaborators data 179 const collaborators = collection.collaboratorIds.map((collaboratorId) => ({ 180 id: new UniqueEntityID().toString(), 181 collectionId: collection.collectionId.getStringValue(), 182 collaboratorId: collaboratorId.value, 183 })); 184 185 // Create card links data 186 const linkPublishedRecords: PublishedRecordDTO[] = []; 187 const cardLinks = collection.cardLinks.map((link) => { 188 let linkPublishedRecordId: string | undefined; 189 190 if (link.publishedRecordId) { 191 const recordId = new UniqueEntityID().toString(); 192 linkPublishedRecords.push({ 193 id: recordId, 194 uri: link.publishedRecordId.uri, 195 cid: link.publishedRecordId.cid, 196 recordedAt: new Date(), 197 }); 198 linkPublishedRecordId = recordId; 199 } 200 201 return { 202 id: new UniqueEntityID().toString(), 203 collectionId: collection.collectionId.getStringValue(), 204 cardId: link.cardId.getStringValue(), 205 addedBy: link.addedBy.value, 206 addedAt: link.addedAt, 207 publishedRecordId: linkPublishedRecordId, 208 }; 209 }); 210 211 return { 212 collection: { 213 id: collection.collectionId.getStringValue(), 214 authorId: collection.authorId.value, 215 name: collection.name.value, 216 description: collection.description?.value, 217 accessType: collection.accessType, 218 cardCount: collection.cardCount, 219 createdAt: collection.createdAt, 220 updatedAt: collection.updatedAt, 221 publishedRecordId, 222 }, 223 collaborators, 224 cardLinks, 225 publishedRecord, 226 linkPublishedRecords: 227 linkPublishedRecords.length > 0 ? linkPublishedRecords : undefined, 228 }; 229 } 230}