Skip to content

Commit 048bea3

Browse files
committed
fix: Changed owner of the jid for instanceName
1 parent eca4285 commit 048bea3

9 files changed

Lines changed: 282 additions & 60 deletions

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
* Adjusted set in webhook to go empty when enabled false
1212
* Adjust in store files
1313
* Fixed the problem when do not save contacts when receive messages
14+
* Changed owner of the jid for instanceName
1415

1516
# 1.1.3 (2023-07-06 11:43)
1617

src/whatsapp/repository/auth.repository.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { IInsert, Repository } from '../abstract/abstract.repository';
44
import { IAuthModel, AuthRaw } from '../models';
55
import { readFileSync } from 'fs';
66
import { AUTH_DIR } from '../../config/path.config';
7-
import { InstanceDto } from '../dto/instance.dto';
7+
import { Logger } from '../../config/logger.config';
88

99
export class AuthRepository extends Repository {
1010
constructor(
@@ -16,24 +16,35 @@ export class AuthRepository extends Repository {
1616
}
1717

1818
private readonly auth: Auth;
19+
private readonly logger = new Logger('AuthRepository');
1920

2021
public async create(data: AuthRaw, instance: string): Promise<IInsert> {
2122
try {
23+
this.logger.verbose('creating auth');
2224
if (this.dbSettings.ENABLED) {
25+
this.logger.verbose('saving auth to db');
2326
const insert = await this.authModel.replaceOne(
2427
{ _id: instance },
2528
{ ...data },
2629
{ upsert: true },
2730
);
31+
32+
this.logger.verbose('auth saved to db: ' + insert.modifiedCount + ' auth');
2833
return { insertCount: insert.modifiedCount };
2934
}
3035

36+
this.logger.verbose('saving auth to store');
37+
3138
this.writeStore<AuthRaw>({
3239
path: join(AUTH_DIR, this.auth.TYPE),
3340
fileName: instance,
3441
data,
3542
});
43+
this.logger.verbose(
44+
'auth saved to store in path: ' + join(AUTH_DIR, this.auth.TYPE) + '/' + instance,
45+
);
3646

47+
this.logger.verbose('auth created');
3748
return { insertCount: 1 };
3849
} catch (error) {
3950
return { error } as any;
@@ -42,10 +53,14 @@ export class AuthRepository extends Repository {
4253

4354
public async find(instance: string): Promise<AuthRaw> {
4455
try {
56+
this.logger.verbose('finding auth');
4557
if (this.dbSettings.ENABLED) {
58+
this.logger.verbose('finding auth in db');
4659
return await this.authModel.findOne({ _id: instance });
4760
}
4861

62+
this.logger.verbose('finding auth in store');
63+
4964
return JSON.parse(
5065
readFileSync(join(AUTH_DIR, this.auth.TYPE, instance + '.json'), {
5166
encoding: 'utf-8',

src/whatsapp/repository/chat.repository.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { ConfigService, StoreConf } from '../../config/env.config';
33
import { IInsert, Repository } from '../abstract/abstract.repository';
44
import { opendirSync, readFileSync, rmSync } from 'fs';
55
import { ChatRaw, IChatModel } from '../models';
6+
import { Logger } from '../../config/logger.config';
67

78
export class ChatQuery {
89
where: ChatRaw;
@@ -16,35 +17,54 @@ export class ChatRepository extends Repository {
1617
super(configService);
1718
}
1819

20+
private readonly logger = new Logger('ChatRepository');
21+
1922
public async insert(
2023
data: ChatRaw[],
2124
instanceName: string,
2225
saveDb = false,
2326
): Promise<IInsert> {
27+
this.logger.verbose('inserting chats');
2428
if (data.length === 0) {
29+
this.logger.verbose('no chats to insert');
2530
return;
2631
}
2732

2833
try {
34+
this.logger.verbose('saving chats to store');
2935
if (this.dbSettings.ENABLED && saveDb) {
36+
this.logger.verbose('saving chats to db');
3037
const insert = await this.chatModel.insertMany([...data]);
38+
39+
this.logger.verbose('chats saved to db: ' + insert.length + ' chats');
3140
return { insertCount: insert.length };
3241
}
3342

43+
this.logger.verbose('saving chats to store');
44+
3445
const store = this.configService.get<StoreConf>('STORE');
3546

3647
if (store.CHATS) {
48+
this.logger.verbose('saving chats to store');
3749
data.forEach((chat) => {
3850
this.writeStore<ChatRaw>({
3951
path: join(this.storePath, 'chats', instanceName),
4052
fileName: chat.id,
4153
data: chat,
4254
});
55+
this.logger.verbose(
56+
'chats saved to store in path: ' +
57+
join(this.storePath, 'chats', instanceName) +
58+
'/' +
59+
chat.id,
60+
);
4361
});
4462

63+
this.logger.verbose('chats saved to store');
4564
return { insertCount: data.length };
4665
}
4766

67+
this.logger.verbose('chats not saved to store');
4868
return { insertCount: 0 };
4969
} catch (error) {
5070
return error;
@@ -55,10 +75,14 @@ export class ChatRepository extends Repository {
5575

5676
public async find(query: ChatQuery): Promise<ChatRaw[]> {
5777
try {
78+
this.logger.verbose('finding chats');
5879
if (this.dbSettings.ENABLED) {
80+
this.logger.verbose('finding chats in db');
5981
return await this.chatModel.find({ owner: query.where.owner });
6082
}
6183

84+
this.logger.verbose('finding chats in store');
85+
6286
const chats: ChatRaw[] = [];
6387
const openDir = opendirSync(join(this.storePath, 'chats', query.where.owner));
6488
for await (const dirent of openDir) {
@@ -74,6 +98,7 @@ export class ChatRepository extends Repository {
7498
}
7599
}
76100

101+
this.logger.verbose('chats found in store: ' + chats.length + ' chats');
77102
return chats;
78103
} catch (error) {
79104
return [];
@@ -82,10 +107,13 @@ export class ChatRepository extends Repository {
82107

83108
public async delete(query: ChatQuery) {
84109
try {
110+
this.logger.verbose('deleting chats');
85111
if (this.dbSettings.ENABLED) {
112+
this.logger.verbose('deleting chats in db');
86113
return await this.chatModel.deleteOne({ ...query.where });
87114
}
88115

116+
this.logger.verbose('deleting chats in store');
89117
rmSync(join(this.storePath, 'chats', query.where.owner, query.where.id + '.josn'), {
90118
force: true,
91119
recursive: true,

src/whatsapp/repository/contact.repository.ts

Lines changed: 73 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { join } from 'path';
33
import { ConfigService, StoreConf } from '../../config/env.config';
44
import { ContactRaw, IContactModel } from '../models';
55
import { IInsert, Repository } from '../abstract/abstract.repository';
6+
import { Logger } from '../../config/logger.config';
67

78
export 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

Comments
 (0)