@@ -3,6 +3,7 @@ import { join } from 'path';
33import { ConfigService , StoreConf } from '../../config/env.config' ;
44import { ContactRaw , IContactModel } from '../models' ;
55import { IInsert , Repository } from '../abstract/abstract.repository' ;
6+ import { Logger } from '../../config/logger.config' ;
67
78export class ContactQuery {
89 where : ContactRaw ;
@@ -16,35 +17,55 @@ export class ContactRepository extends Repository {
1617 super ( configService ) ;
1718 }
1819
20+ private readonly logger = new Logger ( 'ContactRepository' ) ;
21+
1922 public async insert (
2023 data : ContactRaw [ ] ,
2124 instanceName : string ,
2225 saveDb = false ,
2326 ) : Promise < IInsert > {
27+ this . logger . verbose ( 'inserting contacts' ) ;
28+
2429 if ( data . length === 0 ) {
30+ this . logger . verbose ( 'no contacts to insert' ) ;
2531 return ;
2632 }
2733
2834 try {
2935 if ( this . dbSettings . ENABLED && saveDb ) {
36+ this . logger . verbose ( 'saving contacts to db' ) ;
37+
3038 const insert = await this . contactModel . insertMany ( [ ...data ] ) ;
39+
40+ this . logger . verbose ( 'contacts saved to db: ' + insert . length + ' contacts' ) ;
3141 return { insertCount : insert . length } ;
3242 }
3343
44+ this . logger . verbose ( 'saving contacts to store' ) ;
45+
3446 const store = this . configService . get < StoreConf > ( 'STORE' ) ;
3547
3648 if ( store . CONTACTS ) {
49+ this . logger . verbose ( 'saving contacts to store' ) ;
3750 data . forEach ( ( contact ) => {
3851 this . writeStore ( {
3952 path : join ( this . storePath , 'contacts' , instanceName ) ,
4053 fileName : contact . id ,
4154 data : contact ,
4255 } ) ;
56+ this . logger . verbose (
57+ 'contacts saved to store in path: ' +
58+ join ( this . storePath , 'contacts' , instanceName ) +
59+ '/' +
60+ contact . id ,
61+ ) ;
4362 } ) ;
4463
64+ this . logger . verbose ( 'contacts saved to store: ' + data . length + ' contacts' ) ;
4565 return { insertCount : data . length } ;
4666 }
4767
68+ this . logger . verbose ( 'contacts not saved' ) ;
4869 return { insertCount : 0 } ;
4970 } catch ( error ) {
5071 return error ;
@@ -54,31 +75,63 @@ export class ContactRepository extends Repository {
5475 }
5576
5677 public async update (
57- data : ContactRaw ,
78+ data : ContactRaw [ ] ,
5879 instanceName : string ,
5980 saveDb = false ,
6081 ) : Promise < IInsert > {
6182 try {
83+ this . logger . verbose ( 'updating contacts' ) ;
84+
85+ if ( data . length === 0 ) {
86+ this . logger . verbose ( 'no contacts to update' ) ;
87+ return ;
88+ }
89+
6290 if ( this . dbSettings . ENABLED && saveDb ) {
63- const contact = await this . contactModel . findOneAndUpdate (
64- { id : data . id } ,
65- { ...data } ,
66- ) ;
67- return { insertCount : contact ? 1 : 0 } ;
91+ this . logger . verbose ( 'updating contacts in db' ) ;
92+
93+ const contacts = data . map ( ( contact ) => {
94+ return {
95+ updateOne : {
96+ filter : { id : contact . id } ,
97+ update : { ...contact } ,
98+ upsert : true ,
99+ } ,
100+ } ;
101+ } ) ;
102+
103+ const { nModified } = await this . contactModel . bulkWrite ( contacts ) ;
104+
105+ this . logger . verbose ( 'contacts updated in db: ' + nModified + ' contacts' ) ;
106+ return { insertCount : nModified } ;
68107 }
69108
109+ this . logger . verbose ( 'updating contacts in store' ) ;
110+
70111 const store = this . configService . get < StoreConf > ( 'STORE' ) ;
71112
72113 if ( store . CONTACTS ) {
73- this . writeStore ( {
74- path : join ( this . storePath , 'contacts' , instanceName ) ,
75- fileName : data . id ,
76- data,
114+ this . logger . verbose ( 'updating contacts in store' ) ;
115+ data . forEach ( ( contact ) => {
116+ this . writeStore ( {
117+ path : join ( this . storePath , 'contacts' , instanceName ) ,
118+ fileName : contact . id ,
119+ data : contact ,
120+ } ) ;
121+ this . logger . verbose (
122+ 'contacts updated in store in path: ' +
123+ join ( this . storePath , 'contacts' , instanceName ) +
124+ '/' +
125+ contact . id ,
126+ ) ;
77127 } ) ;
78128
79- return { insertCount : 1 } ;
129+ this . logger . verbose ( 'contacts updated in store: ' + data . length + ' contacts' ) ;
130+
131+ return { insertCount : data . length } ;
80132 }
81133
134+ this . logger . verbose ( 'contacts not updated' ) ;
82135 return { insertCount : 0 } ;
83136 } catch ( error ) {
84137 return error ;
@@ -89,11 +142,16 @@ export class ContactRepository extends Repository {
89142
90143 public async find ( query : ContactQuery ) : Promise < ContactRaw [ ] > {
91144 try {
145+ this . logger . verbose ( 'finding contacts' ) ;
92146 if ( this . dbSettings . ENABLED ) {
147+ this . logger . verbose ( 'finding contacts in db' ) ;
93148 return await this . contactModel . find ( { ...query . where } ) ;
94149 }
150+
151+ this . logger . verbose ( 'finding contacts in store' ) ;
95152 const contacts : ContactRaw [ ] = [ ] ;
96153 if ( query ?. where ?. id ) {
154+ this . logger . verbose ( 'finding contacts in store by id' ) ;
97155 contacts . push (
98156 JSON . parse (
99157 readFileSync (
@@ -108,6 +166,8 @@ export class ContactRepository extends Repository {
108166 ) ,
109167 ) ;
110168 } else {
169+ this . logger . verbose ( 'finding contacts in store by owner' ) ;
170+
111171 const openDir = opendirSync ( join ( this . storePath , 'contacts' , query . where . owner ) , {
112172 encoding : 'utf-8' ,
113173 } ) ;
@@ -124,6 +184,8 @@ export class ContactRepository extends Repository {
124184 }
125185 }
126186 }
187+
188+ this . logger . verbose ( 'contacts found in store: ' + contacts . length + ' contacts' ) ;
127189 return contacts ;
128190 } catch ( error ) {
129191 return [ ] ;
0 commit comments