File size: 1,448 Bytes
d76f93d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import { readMyData } from "../utils/fileStorage";
import { Book, bookLinkManager } from "./Book";
import { Link } from "./Link";
import { LinkManager } from "./LinkManager";

export interface Visitor {
   id: string;
   name: string;
   surname: string;
   registrationDate: string;
   currentBooks: Link[];
   history: Link[];
}

export class VisitorLinkManager extends LinkManager<Visitor> {
   protected fileName = "visitors.sea";
   protected tableName = "visitors";

   async enrichWithBooks(visitor: Visitor): Promise<VisitorWithBooks> {

      const currentBooks = await bookLinkManager.resolveMany(visitor.currentBooks);
      const history = await bookLinkManager.resolveMany(visitor.history);

      return {
         id: visitor.id,
         name: visitor.name,
         surname: visitor.surname,
         registrationDate: visitor.registrationDate,
         currentBooks,
         history,
      };
   }

   async getAllEnriched(): Promise<VisitorWithBooks[]> {
      const visitors = await readMyData<Visitor>(this.fileName);
      return await Promise.all(
         visitors.map((v) => this.enrichWithBooks(v))
      );
   }
}

export const visitorLinkManager = new VisitorLinkManager(); 

export interface VisitorWithBooks extends Omit<Visitor, 'currentBooks' | 'history'> {
   currentBooks: Book[];
   history: Book[];
}

export interface CreateVisitorRequest {
   name: string ;
   surname: string;
   registrationDate: string;
}