11import { BaseService } from '@app/common/base/base.service'
2+ import { Reference } from '@app/common/typings/mongodb'
23import {
34 getWalletEns ,
45 getWalletFarcasterProfile ,
@@ -7,16 +8,20 @@ import {
78} from '@app/definitions/utils/address.utils'
89import { Injectable , Logger } from '@nestjs/common'
910import { ReturnModelType } from '@typegoose/typegoose'
11+ import { uniq } from 'lodash'
12+ import { ObjectId } from 'mongodb'
1013import { InjectModel } from 'nestjs-typegoose'
1114import { User } from '../../users/entities/user'
1215import { Contact } from '../entities/contact'
1316
1417@Injectable ( )
1518export class ContactService extends BaseService < Contact > {
1619 protected readonly logger = new Logger ( ContactService . name )
20+ static instance : ContactService
1721
1822 constructor ( @InjectModel ( Contact ) protected readonly model : ReturnModelType < typeof Contact > ) {
1923 super ( model )
24+ ContactService . instance = this
2025 }
2126
2227 async resolveContactData ( address : string , key : string , contactOwner : User ) {
@@ -34,4 +39,49 @@ export class ContactService extends BaseService<Contact> {
3439 return contact ?. tags ?? [ ]
3540 }
3641 }
42+
43+ async addTags ( address : string , tags : string [ ] , ownerId : ObjectId ) : Promise < string [ ] > {
44+ const contact = await this . findOne ( {
45+ owner : ownerId ,
46+ address,
47+ } )
48+ if ( ! contact ) {
49+ await this . createOne ( {
50+ owner : ownerId as Reference < User > ,
51+ address,
52+ tags,
53+ } )
54+ return tags
55+ }
56+ if ( tags . length ) {
57+ const newTags = uniq ( [ ...contact . tags , ...tags ] )
58+ if ( newTags . length !== contact . tags . length ) {
59+ await this . updateById ( contact . _id , {
60+ tags : newTags ,
61+ } )
62+ return newTags
63+ }
64+ }
65+ return contact . tags
66+ }
67+
68+ async removeTags ( address : string , tags : string [ ] , ownerId : ObjectId ) : Promise < string [ ] > {
69+ const contact = await this . findOne ( {
70+ owner : ownerId ,
71+ address,
72+ } )
73+ if ( ! contact ) {
74+ return [ ]
75+ }
76+ if ( tags . length ) {
77+ const newTags = contact . tags . filter ( ( tag ) => ! tags . includes ( tag ) )
78+ if ( newTags . length !== contact . tags . length ) {
79+ await this . updateById ( contact . _id , {
80+ tags : newTags ,
81+ } )
82+ return newTags
83+ }
84+ }
85+ return contact . tags
86+ }
3787}
0 commit comments