@things-factory/contact 6.0.7 → 6.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@things-factory/contact",
3
- "version": "6.0.7",
3
+ "version": "6.0.8",
4
4
  "main": "dist-server/index.js",
5
5
  "browser": "dist-client/index.js",
6
6
  "things-factory": true,
@@ -34,5 +34,5 @@
34
34
  "@things-factory/auth-base": "^6.0.7",
35
35
  "@things-factory/shell": "^6.0.7"
36
36
  },
37
- "gitHead": "5af6d8613bd6eab6feaa69df942ef3ddfae764fe"
37
+ "gitHead": "c883904927d790f21f9e821ddbb4b74be370040e"
38
38
  }
@@ -1,33 +1,17 @@
1
- import { Column, Entity, PrimaryGeneratedColumn, ManyToOne, RelationId } from 'typeorm'
2
- import { ObjectType, Field, Int, ID, InputType, registerEnumType } from 'type-graphql'
1
+ import { ObjectType, Field, InputType } from 'type-graphql'
3
2
 
4
- import { Contact, ContactField } from './contact'
3
+ import { ContactField } from './contact'
5
4
 
6
- @Entity()
7
- @ObjectType({ description: 'Entity for ContactItem' })
5
+ @ObjectType()
8
6
  export class ContactItem {
9
- @PrimaryGeneratedColumn('uuid')
10
- @Field(type => ID)
11
- readonly id: string
12
-
13
- @Column()
14
7
  @Field()
15
8
  label: string
16
9
 
17
- @Column()
18
10
  @Field()
19
11
  type: ContactField
20
12
 
21
- @Column()
22
13
  @Field()
23
14
  value: string
24
-
25
- @ManyToOne(type => Contact, contact => contact.items, { onDelete: 'CASCADE' })
26
- @Field()
27
- contact: Contact
28
-
29
- @RelationId((item: ContactItem) => item.contact)
30
- contactId: string
31
15
  }
32
16
 
33
17
  @InputType()
@@ -4,7 +4,6 @@ import { In } from 'typeorm'
4
4
  import { createAttachment, deleteAttachmentsByRef } from '@things-factory/attachment-base'
5
5
 
6
6
  import { Contact } from './contact'
7
- import { ContactItem } from './contact-item'
8
7
  import { NewContact, ContactPatch } from './contact-type'
9
8
 
10
9
  @Resolver(Contact)
@@ -21,7 +20,6 @@ export class ContactMutation {
21
20
  }
22
21
 
23
22
  const ContactRepository = tx.getRepository(Contact)
24
- const ContactItemRepository = tx.getRepository(ContactItem)
25
23
 
26
24
  const result = await ContactRepository.save({
27
25
  ...contact,
@@ -30,13 +28,6 @@ export class ContactMutation {
30
28
  updater: user
31
29
  })
32
30
 
33
- for (let item of contact.items || []) {
34
- await ContactItemRepository.save({
35
- ...item,
36
- contact: { id: result.id }
37
- })
38
- }
39
-
40
31
  if (file) {
41
32
  await createAttachment(
42
33
  null,
@@ -75,22 +66,6 @@ export class ContactMutation {
75
66
  where: { domain: { id: domain.id }, id }
76
67
  })
77
68
 
78
- if (patch.items) {
79
- const ContactItemRepository = tx.getRepository(ContactItem)
80
- await ContactItemRepository.delete({
81
- contact: { id: contact.id }
82
- })
83
-
84
- for (let item of patch.items) {
85
- await ContactItemRepository.save({
86
- ...item,
87
- contact: { id: contact.id }
88
- })
89
- }
90
-
91
- delete patch.items
92
- }
93
-
94
69
  const result = await ContactRepository.save({
95
70
  ...contact,
96
71
  ...patch,
@@ -7,6 +7,22 @@ import { ContactItem } from './contact-item'
7
7
  import { Profile } from './profile'
8
8
  import { ContactList } from './contact-type'
9
9
 
10
+ function getContactItems(contact: Contact) {
11
+ const { items } = contact
12
+
13
+ if (!items || !(items instanceof Array)) {
14
+ return null
15
+ }
16
+
17
+ return items
18
+ }
19
+
20
+ function getContactItem(contact: Contact, type: String, label: String) {
21
+ const { items } = contact
22
+
23
+ return items?.find(item => item.type === type && item.label === label)?.value || ''
24
+ }
25
+
10
26
  @Resolver(Contact)
11
27
  export class ContactQuery {
12
28
  @Query(returns => Contact!, { nullable: true, description: 'To fetch a Contact' })
@@ -54,48 +70,28 @@ export class ContactQuery {
54
70
  }
55
71
 
56
72
  @FieldResolver(type => String)
57
- async email(@Root() contact: Contact): Promise<string> {
58
- const email = await getRepository(ContactItem).findOneBy({
59
- contact: { id: contact.id },
60
- type: ContactField.Email,
61
- label: 'work'
62
- })
63
- return email?.value
73
+ async phone(@Root() contact: Contact): Promise<string> {
74
+ return getContactItem(contact, ContactField.Phone, 'work')
64
75
  }
65
76
 
66
77
  @FieldResolver(type => String)
67
- async phone(@Root() contact: Contact): Promise<string> {
68
- const phone = await getRepository(ContactItem).findOneBy({
69
- contact: { id: contact.id },
70
- type: ContactField.Phone,
71
- label: 'work'
72
- })
73
- return phone?.value
78
+ async email(@Root() contact: Contact): Promise<string> {
79
+ return getContactItem(contact, ContactField.Email, 'work')
74
80
  }
75
81
 
76
82
  @FieldResolver(type => String)
77
83
  async address(@Root() contact: Contact): Promise<string> {
78
- const address = await getRepository(ContactItem).findOneBy({
79
- contact: { id: contact.id },
80
- type: ContactField.Address,
81
- label: 'work'
82
- })
83
- return address?.value
84
+ return getContactItem(contact, ContactField.Address, 'work')
84
85
  }
85
86
 
86
87
  @FieldResolver(type => String)
87
88
  async department(@Root() contact: Contact): Promise<string> {
88
- const department = await getRepository(ContactItem).findOneBy({
89
- contact: { id: contact.id },
90
- type: ContactField.Department,
91
- label: 'work'
92
- })
93
- return department?.value
89
+ return getContactItem(contact, ContactField.Department, 'work')
94
90
  }
95
91
 
96
92
  @FieldResolver(type => [ContactItem])
97
93
  async items(@Root() contact: Contact): Promise<ContactItem[]> {
98
- return await getRepository(ContactItem).findBy({ contact: { id: contact.id } })
94
+ return getContactItems(contact)
99
95
  }
100
96
 
101
97
  @FieldResolver(type => Domain)
@@ -79,8 +79,8 @@ export class Contact {
79
79
  @Field(type => Profile, { nullable: true })
80
80
  profile: Profile
81
81
 
82
- @OneToMany(type => ContactItem, contactItem => contactItem.contact)
83
- @Field(type => [ContactItem])
82
+ @Column('simple-json', { nullable: true })
83
+ @Field(type => [ContactItem], { nullable: true })
84
84
  items?: ContactItem[]
85
85
 
86
86
  @CreateDateColumn()
@@ -1,8 +1,7 @@
1
1
  import { Contact } from './contact'
2
- import { ContactItem } from './contact-item'
3
2
  import { Profile } from './profile'
4
3
  import { ContactQuery } from './contact-query'
5
4
  import { ContactMutation } from './contact-mutation'
6
5
 
7
- export const entities = [Profile, Contact, ContactItem]
6
+ export const entities = [Profile, Contact]
8
7
  export const resolvers = [ContactQuery, ContactMutation]