File size: 2,383 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import { GraphQL } from './graphql.ts'

function updateMutName(typeName: string): string {
  return 'update' + typeName.charAt(0).toUpperCase() + typeName.slice(1)
}

function guessType(val: unknown): string {
  switch (typeof val) {
    case 'string':
      return 'String'
    case 'number':
      return 'Int'
    case 'boolean':
      return 'Boolean'
    default:
      throw new Error(`Unknown type: ${typeof val}`)
  }
}

// Entity is a base class for all entities in the system (e.g., schedules, rotations, users, etc.)
export class BaseEntity {
  constructor(
    private gql: GraphQL,
    private typeName: string,
    public readonly id: string,
  ) {}

  protected getField(fieldName: string): unknown {
    return this.gql.query(
      `query GetField($id: ID!){base: ${this.typeName}(id: $id){value: ${fieldName}}}`,
      { id: this.id },
    ).base.value
  }

  // This method is used to set a field on the entity.
  //
  // Example: to set the name of a rotation, you would call `setField('name', 'new name')`.
  protected setField(
    fieldName: string,
    value: unknown,
    typeName: string = guessType(value),
  ): void {
    this.gql.query(
      `mutation SetField($id: ID!, $value: ${typeName}!){${updateMutName(this.typeName)}(input: {id: $id, ${fieldName}: $value})}`,
      { id: this.id, value },
    )
  }

  public delete(): void {
    this.gql.query(
      `mutation Delete($tgt: TargetInput!){deleteAll(input: [$tgt])}`,
      {
        tgt: {
          type: this.typeName,
          id: this.id,
        },
      },
    )
  }

  public get name(): string {
    return this.getField('name') as string
  }

  public set name(value: string) {
    this.setField('name', value)
  }

  public get isFavorite(): boolean {
    return this.getField('isFavorite') as boolean
  }

  public set isFavorite(value: boolean) {
    this.gql.query(
      `mutation SetIsFavorite($tgt: TargetInput!, $value: Boolean!){setFavorite(input: {target: $tgt, isFavorite: $value})}`,
      { tgt: { id: this.id, type: this.typeName }, value },
    )
  }
}

export class Entity extends BaseEntity {
  constructor(gql: GraphQL, typeName: string, id: string) {
    super(gql, typeName, id)
  }

  public get description(): string {
    return this.getField('description') as string
  }

  public set description(value: string) {
    this.setField('description', value)
  }
}