@@ -16,6 +16,7 @@ import { uniq } from 'lodash'
1616import { ObjectId , WriteError } from 'mongodb'
1717import { InjectModel } from 'nestjs-typegoose'
1818import { User } from '../../users/entities/user'
19+ import { UserService } from '../../users/services/user.service'
1920import { Contact } from '../entities/contact'
2021
2122@Injectable ( )
@@ -26,6 +27,7 @@ export class ContactService extends BaseService<Contact> {
2627 constructor (
2728 @InjectModel ( Contact ) protected readonly model : ReturnModelType < typeof Contact > ,
2829 @InjectQueue ( 'contacts' ) private contactsQueue : Queue ,
30+ private userService : UserService ,
2931 ) {
3032 super ( model )
3133 ContactService . instance = this
@@ -140,6 +142,7 @@ export class ContactService extends BaseService<Contact> {
140142 const existingTags = contact . tags . map ( ( tag ) => tag . toLowerCase ( ) )
141143 const tagsAdded = tags . filter ( ( tag ) => ! existingTags . includes ( tag . toLowerCase ( ) ) )
142144 await this . onTagsAdded ( contact , tagsAdded )
145+ await this . userService . addContactDataKeys ( contact . owner . _id , [ contact ] )
143146 return newTags
144147 }
145148 }
@@ -176,6 +179,7 @@ export class ContactService extends BaseService<Contact> {
176179 if ( contact . tags ?. length ) {
177180 await this . onTagsAdded ( contact , contact . tags )
178181 }
182+ await this . userService . addContactDataKeys ( contact . owner . _id , [ contact ] )
179183 }
180184
181185 async afterCreateMany ( contacts : Contact [ ] ) {
@@ -195,14 +199,15 @@ export class ContactService extends BaseService<Contact> {
195199 tags : contactsWithTags . flatMap ( ( contact ) => contact . tags ) ,
196200 } )
197201 }
202+ await this . userService . addContactDataKeys ( contacts [ 0 ] . owner . _id , contacts )
198203 }
199204
200205 async updateOne (
201206 id : string ,
202207 update : Partial < Contact > ,
203208 opts ?: UpdateOneOptions < Contact > | undefined ,
204209 ) : Promise < Contact > {
205- if ( ! update . tags ) {
210+ if ( ! update . tags && ! update . fields ) {
206211 return super . updateOne ( id , update , opts )
207212 }
208213
@@ -212,11 +217,17 @@ export class ContactService extends BaseService<Contact> {
212217 throw new NotFoundException ( `Contact with id ${ id } not found` )
213218 }
214219 const contact = await super . updateOne ( id , update , opts )
215- const existingTags = contactBefore . tags . map ( ( tag ) => tag . toLowerCase ( ) )
216- const tagsAdded = update . tags . filter ( ( tag ) => ! existingTags . includes ( tag . toLowerCase ( ) ) )
217- if ( tagsAdded ?. length ) {
218- await this . onTagsAdded ( contact , tagsAdded )
220+
221+ if ( update . tags ) {
222+ const existingTags = contactBefore . tags . map ( ( tag ) => tag . toLowerCase ( ) )
223+ const tagsAdded = update . tags . filter ( ( tag ) => ! existingTags . includes ( tag . toLowerCase ( ) ) )
224+ if ( tagsAdded ?. length ) {
225+ await this . onTagsAdded ( contact , tagsAdded )
226+ }
219227 }
228+
229+ await this . userService . addContactDataKeys ( contact . owner . _id , [ contact ] )
230+
220231 return contact
221232 }
222233
0 commit comments