This repository has no description
1import { Result, ok, err } from '../../../../shared/core/Result';
2import {
3 IAtUriResolutionService,
4 AtUriResourceType,
5 AtUriResolutionResult,
6} from '../../domain/services/IAtUriResolutionService';
7import { CollectionId } from '../../domain/value-objects/CollectionId';
8import { CardId } from '../../domain/value-objects/CardId';
9import { InMemoryCollectionRepository } from './InMemoryCollectionRepository';
10import { InMemoryCardRepository } from './InMemoryCardRepository';
11
12export class InMemoryAtUriResolutionService implements IAtUriResolutionService {
13 constructor(
14 private collectionRepository: InMemoryCollectionRepository,
15 private cardRepository: InMemoryCardRepository,
16 ) {}
17
18 async resolveAtUri(
19 atUri: string,
20 ): Promise<Result<AtUriResolutionResult | null>> {
21 try {
22 // Check cards first
23 const allCards = this.cardRepository.getAllCards();
24 for (const card of allCards) {
25 if (card.publishedRecordId?.uri === atUri) {
26 return ok({
27 type: AtUriResourceType.CARD,
28 id: card.cardId,
29 });
30 }
31 }
32
33 // Check collections
34 const allCollections = this.collectionRepository.getAllCollections();
35 for (const collection of allCollections) {
36 if (collection.publishedRecordId?.uri === atUri) {
37 return ok({
38 type: AtUriResourceType.COLLECTION,
39 id: collection.collectionId,
40 });
41 }
42
43 // Check collection links (cards within collections)
44 for (const cardLink of collection.cardLinks) {
45 if (cardLink.publishedRecordId?.uri === atUri) {
46 return ok({
47 type: AtUriResourceType.COLLECTION_LINK,
48 id: {
49 collectionId: collection.collectionId,
50 cardId: cardLink.cardId,
51 },
52 });
53 }
54 }
55 }
56
57 return ok(null);
58 } catch (error) {
59 return err(error as Error);
60 }
61 }
62
63 async resolveCollectionId(
64 atUri: string,
65 ): Promise<Result<CollectionId | null>> {
66 const result = await this.resolveAtUri(atUri);
67
68 if (result.isErr()) {
69 return err(result.error);
70 }
71
72 if (!result.value || result.value.type !== AtUriResourceType.COLLECTION) {
73 return ok(null);
74 }
75
76 return ok(result.value.id as CollectionId);
77 }
78
79 async resolveCardId(atUri: string): Promise<Result<CardId | null>> {
80 const result = await this.resolveAtUri(atUri);
81
82 if (result.isErr()) {
83 return err(result.error);
84 }
85
86 if (!result.value || result.value.type !== AtUriResourceType.CARD) {
87 return ok(null);
88 }
89
90 return ok(result.value.id as CardId);
91 }
92
93 async resolveCollectionLinkId(
94 atUri: string,
95 ): Promise<Result<{ collectionId: CollectionId; cardId: CardId } | null>> {
96 const result = await this.resolveAtUri(atUri);
97
98 if (result.isErr()) {
99 return err(result.error);
100 }
101
102 if (
103 !result.value ||
104 result.value.type !== AtUriResourceType.COLLECTION_LINK
105 ) {
106 return ok(null);
107 }
108
109 return ok(
110 result.value.id as { collectionId: CollectionId; cardId: CardId },
111 );
112 }
113}