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';
10
11export class InMemoryAtUriResolutionService implements IAtUriResolutionService {
12 constructor(private collectionRepository: InMemoryCollectionRepository) {}
13
14 async resolveAtUri(
15 atUri: string,
16 ): Promise<Result<AtUriResolutionResult | null>> {
17 try {
18 // Get all collections and check if any have a published record with this URI
19 const allCollections = this.collectionRepository.getAllCollections();
20
21 for (const collection of allCollections) {
22 if (collection.publishedRecordId?.uri === atUri) {
23 return ok({
24 type: AtUriResourceType.COLLECTION,
25 id: collection.collectionId,
26 });
27 }
28 }
29
30 return ok(null);
31 } catch (error) {
32 return err(error as Error);
33 }
34 }
35
36 async resolveCollectionId(
37 atUri: string,
38 ): Promise<Result<CollectionId | null>> {
39 const result = await this.resolveAtUri(atUri);
40
41 if (result.isErr()) {
42 return err(result.error);
43 }
44
45 if (!result.value || result.value.type !== AtUriResourceType.COLLECTION) {
46 return ok(null);
47 }
48
49 return ok(result.value.id as CollectionId);
50 }
51
52 async resolveCardId(atUri: string): Promise<Result<CardId | null>> {
53 const result = await this.resolveAtUri(atUri);
54
55 if (result.isErr()) {
56 return err(result.error);
57 }
58
59 if (!result.value || result.value.type !== AtUriResourceType.CARD) {
60 return ok(null);
61 }
62
63 return ok(result.value.id as CardId);
64 }
65
66 async resolveCollectionLinkId(
67 atUri: string,
68 ): Promise<Result<{ collectionId: CollectionId; cardId: CardId } | null>> {
69 const result = await this.resolveAtUri(atUri);
70
71 if (result.isErr()) {
72 return err(result.error);
73 }
74
75 if (
76 !result.value ||
77 result.value.type !== AtUriResourceType.COLLECTION_LINK
78 ) {
79 return ok(null);
80 }
81
82 return ok(
83 result.value.id as { collectionId: CollectionId; cardId: CardId },
84 );
85 }
86}