zync-nest-data-module 1.0.0
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/README.md +672 -0
- package/dist/backup/backup.config.d.ts +4 -0
- package/dist/backup/backup.config.js +84 -0
- package/dist/backup/backup.config.js.map +1 -0
- package/dist/backup/backup.interface.d.ts +21 -0
- package/dist/backup/backup.interface.js +3 -0
- package/dist/backup/backup.interface.js.map +1 -0
- package/dist/backup/backup.module.d.ts +2 -0
- package/dist/backup/backup.module.js +24 -0
- package/dist/backup/backup.module.js.map +1 -0
- package/dist/backup/backup.service.d.ts +19 -0
- package/dist/backup/backup.service.js +229 -0
- package/dist/backup/backup.service.js.map +1 -0
- package/dist/backup/index.d.ts +4 -0
- package/dist/backup/index.js +21 -0
- package/dist/backup/index.js.map +1 -0
- package/dist/database/database.module.d.ts +2 -0
- package/dist/database/database.module.js +34 -0
- package/dist/database/database.module.js.map +1 -0
- package/dist/database/database.repository.d.ts +62 -0
- package/dist/database/database.repository.js +259 -0
- package/dist/database/database.repository.js.map +1 -0
- package/dist/database/database.scheme.d.ts +45 -0
- package/dist/database/database.scheme.js +187 -0
- package/dist/database/database.scheme.js.map +1 -0
- package/dist/database/database.service.d.ts +7 -0
- package/dist/database/database.service.js +39 -0
- package/dist/database/database.service.js.map +1 -0
- package/dist/database/database.sync.d.ts +10 -0
- package/dist/database/database.sync.js +44 -0
- package/dist/database/database.sync.js.map +1 -0
- package/dist/database/database.transaction.d.ts +17 -0
- package/dist/database/database.transaction.js +101 -0
- package/dist/database/database.transaction.js.map +1 -0
- package/dist/database/database.uniqueId.d.ts +25 -0
- package/dist/database/database.uniqueId.js +68 -0
- package/dist/database/database.uniqueId.js.map +1 -0
- package/dist/database/database.utils.d.ts +10 -0
- package/dist/database/database.utils.js +119 -0
- package/dist/database/database.utils.js.map +1 -0
- package/dist/database/index.d.ts +8 -0
- package/dist/database/index.js +25 -0
- package/dist/database/index.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +20 -0
- package/dist/index.js.map +1 -0
- package/dist/service/index.d.ts +1 -0
- package/dist/service/index.js +18 -0
- package/dist/service/index.js.map +1 -0
- package/dist/service/service.d.ts +40 -0
- package/dist/service/service.js +182 -0
- package/dist/service/service.js.map +1 -0
- package/dist/tsconfig.lib.tsbuildinfo +1 -0
- package/libs/src/app.controller.ts +84 -0
- package/libs/src/app.module.ts +31 -0
- package/libs/src/backup/backup.config.ts +45 -0
- package/libs/src/backup/backup.interface.ts +21 -0
- package/libs/src/backup/backup.module.ts +11 -0
- package/libs/src/backup/backup.service.ts +283 -0
- package/libs/src/backup/index.ts +4 -0
- package/libs/src/database/database.module.ts +26 -0
- package/libs/src/database/database.repository.ts +358 -0
- package/libs/src/database/database.scheme.ts +128 -0
- package/libs/src/database/database.service.ts +36 -0
- package/libs/src/database/database.sync.ts +61 -0
- package/libs/src/database/database.transaction.ts +101 -0
- package/libs/src/database/database.uniqueId.ts +59 -0
- package/libs/src/database/database.utils.ts +99 -0
- package/libs/src/database/index.ts +8 -0
- package/libs/src/index.ts +3 -0
- package/libs/src/main.ts +62 -0
- package/libs/src/service/index.ts +1 -0
- package/libs/src/service/service.ts +201 -0
- package/libs/src/test/test.dto.ts +41 -0
- package/libs/src/test/test.module.ts +20 -0
- package/libs/src/test/test.repository.ts +44 -0
- package/libs/src/test/test.resolver.ts +44 -0
- package/libs/src/test/test.schema.ts +21 -0
- package/libs/src/test/test.service.ts +19 -0
- package/libs/tsconfig.lib.json +19 -0
- package/nest-cli.json +16 -0
- package/package.json +89 -0
- package/tsconfig.json +29 -0
- package/update-links.js +159 -0
package/README.md
ADDED
|
@@ -0,0 +1,672 @@
|
|
|
1
|
+
# Zync NestJS Library
|
|
2
|
+
|
|
3
|
+
A comprehensive NestJS library providing database backup utilities, file upload services, and common utilities for modern web applications.
|
|
4
|
+
|
|
5
|
+
## 📦 Features
|
|
6
|
+
|
|
7
|
+
- **Database Backup Service**: Automated MongoDB backup with cloud storage support
|
|
8
|
+
- **File Upload Service**: Handle file uploads with image resizing and cloud storage
|
|
9
|
+
- **Bucket Service**: AWS S3/DigitalOcean Spaces integration
|
|
10
|
+
- **Firebase Service**: Push notification service with FCM integration
|
|
11
|
+
- **Mailer Service**: Email sending service with template support
|
|
12
|
+
- **Exabytes SMS Service**: SMS sending service via Exabytes API
|
|
13
|
+
- **UltraMsg Service**: WhatsApp messaging service via UltraMsg API
|
|
14
|
+
- **Message Service**: Unified messaging service for various channels
|
|
15
|
+
- **Utility Functions**: Common helpers for date formatting, file handling, and more
|
|
16
|
+
|
|
17
|
+
## 🚀 Installation
|
|
18
|
+
|
|
19
|
+
### Local Development
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
# Clone the repository
|
|
23
|
+
git clone <repository-url>
|
|
24
|
+
cd zync-nest-library
|
|
25
|
+
|
|
26
|
+
# Install dependencies
|
|
27
|
+
pnpm install
|
|
28
|
+
|
|
29
|
+
# Build the library
|
|
30
|
+
pnpm run build
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
### Using in Other Projects
|
|
34
|
+
|
|
35
|
+
#### Method 1: File Path Dependency (Recommended for pnpm)
|
|
36
|
+
|
|
37
|
+
In your project's `package.json`:
|
|
38
|
+
|
|
39
|
+
```json
|
|
40
|
+
{
|
|
41
|
+
"dependencies": {
|
|
42
|
+
"zync-nest-library": "file:../../zync-library/zync-nest-library"
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
Then run:
|
|
48
|
+
```bash
|
|
49
|
+
pnpm install
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
#### Method 2: npm link (For npm users)
|
|
53
|
+
|
|
54
|
+
```bash
|
|
55
|
+
# In the library directory
|
|
56
|
+
npm link
|
|
57
|
+
|
|
58
|
+
# In your consuming project
|
|
59
|
+
npm link zync-nest-library
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
#### Method 3: pnpm link (Alternative for pnpm)
|
|
63
|
+
|
|
64
|
+
```bash
|
|
65
|
+
# Setup pnpm global bin directory (one-time setup)
|
|
66
|
+
pnpm setup
|
|
67
|
+
|
|
68
|
+
# In the library directory
|
|
69
|
+
pnpm link --global
|
|
70
|
+
|
|
71
|
+
# In your consuming project
|
|
72
|
+
pnpm link --global zync-nest-library
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
## 📚 Usage
|
|
76
|
+
|
|
77
|
+
### Basic Setup
|
|
78
|
+
|
|
79
|
+
Import the required modules in your NestJS application:
|
|
80
|
+
|
|
81
|
+
```typescript
|
|
82
|
+
import { Module } from '@nestjs/common';
|
|
83
|
+
import {
|
|
84
|
+
ApDbBackupModule,
|
|
85
|
+
UploadModule,
|
|
86
|
+
FirebaseModule,
|
|
87
|
+
MailerModule,
|
|
88
|
+
MessageModule
|
|
89
|
+
} from 'zync-nest-library';
|
|
90
|
+
|
|
91
|
+
@Module({
|
|
92
|
+
imports: [
|
|
93
|
+
ApDbBackupModule,
|
|
94
|
+
UploadModule,
|
|
95
|
+
FirebaseModule,
|
|
96
|
+
MailerModule,
|
|
97
|
+
MessageModule,
|
|
98
|
+
// ... other modules
|
|
99
|
+
],
|
|
100
|
+
})
|
|
101
|
+
export class AppModule {}
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
### Database Backup Service
|
|
105
|
+
|
|
106
|
+
The `DbBackupService` provides automated MongoDB backup functionality with optional cloud storage.
|
|
107
|
+
|
|
108
|
+
#### Configuration
|
|
109
|
+
|
|
110
|
+
```typescript
|
|
111
|
+
import { IDbBackupConfig } from 'zync-nest-library';
|
|
112
|
+
|
|
113
|
+
const backupConfig: IDbBackupConfig = {
|
|
114
|
+
env: 'production',
|
|
115
|
+
mongodb: {
|
|
116
|
+
connectionStrings: 'mongodb://localhost:27017/mydb'
|
|
117
|
+
// or multiple connections: ['mongodb://...', 'mongodb://...']
|
|
118
|
+
},
|
|
119
|
+
backup: {
|
|
120
|
+
dir: './backups',
|
|
121
|
+
enabled: true,
|
|
122
|
+
maxBackups: 10,
|
|
123
|
+
isDownload: false,
|
|
124
|
+
uploadToSpace: true
|
|
125
|
+
},
|
|
126
|
+
spaces: {
|
|
127
|
+
name: 'my-bucket',
|
|
128
|
+
region: 'nyc3',
|
|
129
|
+
key: 'your-access-key',
|
|
130
|
+
secret: 'your-secret-key',
|
|
131
|
+
endpoint: 'https://nyc3.digitaloceanspaces.com',
|
|
132
|
+
dir: 'backups'
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
#### Usage
|
|
138
|
+
|
|
139
|
+
```typescript
|
|
140
|
+
import { Injectable } from '@nestjs/common';
|
|
141
|
+
import { DbBackupService } from 'zync-nest-library';
|
|
142
|
+
|
|
143
|
+
@Injectable()
|
|
144
|
+
export class MyService {
|
|
145
|
+
constructor(private readonly backupService: DbBackupService) {}
|
|
146
|
+
|
|
147
|
+
async createBackup() {
|
|
148
|
+
await this.backupService.backup();
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
async scheduleBackups() {
|
|
152
|
+
// Set up automated backups (runs every 6 hours by default)
|
|
153
|
+
this.backupService.startBackupSchedule();
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
### Upload Service
|
|
159
|
+
|
|
160
|
+
The `UploadService` handles file uploads with automatic image resizing and cloud storage integration.
|
|
161
|
+
|
|
162
|
+
#### Interfaces
|
|
163
|
+
|
|
164
|
+
```typescript
|
|
165
|
+
import { IUpload, IUploadResult } from 'zync-nest-library';
|
|
166
|
+
|
|
167
|
+
// Upload interface
|
|
168
|
+
interface IUpload {
|
|
169
|
+
type?: "base64" | "buffer";
|
|
170
|
+
file: any;
|
|
171
|
+
filepath?: string;
|
|
172
|
+
filename: string;
|
|
173
|
+
filetype: string;
|
|
174
|
+
dir?: string;
|
|
175
|
+
transformName?: boolean;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// Upload result
|
|
179
|
+
interface IUploadResult {
|
|
180
|
+
name: string;
|
|
181
|
+
type: string;
|
|
182
|
+
uri: string;
|
|
183
|
+
lgUri?: string; // Large thumbnail
|
|
184
|
+
mdUri?: string; // Medium thumbnail
|
|
185
|
+
smUri?: string; // Small thumbnail
|
|
186
|
+
}
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
#### Usage
|
|
190
|
+
|
|
191
|
+
```typescript
|
|
192
|
+
import { Injectable } from '@nestjs/common';
|
|
193
|
+
import { UploadService } from 'zync-nest-library';
|
|
194
|
+
|
|
195
|
+
@Injectable()
|
|
196
|
+
export class FileService {
|
|
197
|
+
constructor(private readonly uploadService: UploadService) {}
|
|
198
|
+
|
|
199
|
+
async uploadFile(file: Express.Multer.File): Promise<IUploadResult> {
|
|
200
|
+
const uploadData: IUpload = {
|
|
201
|
+
type: 'buffer',
|
|
202
|
+
file: file.buffer,
|
|
203
|
+
filename: file.originalname,
|
|
204
|
+
filetype: file.mimetype,
|
|
205
|
+
dir: 'uploads',
|
|
206
|
+
transformName: true
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
return await this.uploadService.uploadFile(uploadData);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
async uploadBase64Image(base64: string): Promise<IUploadResult> {
|
|
213
|
+
const uploadData: IUpload = {
|
|
214
|
+
type: 'base64',
|
|
215
|
+
file: base64,
|
|
216
|
+
filename: 'image.jpg',
|
|
217
|
+
filetype: 'image/jpeg',
|
|
218
|
+
dir: 'images'
|
|
219
|
+
};
|
|
220
|
+
|
|
221
|
+
return await this.uploadService.uploadFile(uploadData);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
### Bucket Service
|
|
227
|
+
|
|
228
|
+
The `BucketService` provides direct integration with cloud storage services (AWS S3, DigitalOcean Spaces).
|
|
229
|
+
|
|
230
|
+
#### Configuration
|
|
231
|
+
|
|
232
|
+
```typescript
|
|
233
|
+
// In your environment or config
|
|
234
|
+
export default () => ({
|
|
235
|
+
bucket: {
|
|
236
|
+
name: process.env.aws_bucket,
|
|
237
|
+
region: process.env.aws_s3_region,
|
|
238
|
+
key: process.env.aws_access_key_id,
|
|
239
|
+
secret: process.env.aws_secret_access_key,
|
|
240
|
+
endpoint: process.env.aws_s3_endpoint,
|
|
241
|
+
}
|
|
242
|
+
});
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
#### Usage
|
|
246
|
+
|
|
247
|
+
```typescript
|
|
248
|
+
import { Injectable } from '@nestjs/common';
|
|
249
|
+
import { BucketService } from 'zync-nest-library';
|
|
250
|
+
|
|
251
|
+
@Injectable()
|
|
252
|
+
export class CloudStorageService {
|
|
253
|
+
constructor(private readonly bucketService: BucketService) {}
|
|
254
|
+
|
|
255
|
+
async uploadToBucket(buffer: Buffer, key: string) {
|
|
256
|
+
return await this.bucketService.upload(buffer, key);
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
async downloadFromBucket(key: string) {
|
|
260
|
+
return await this.bucketService.download(key);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
async deleteFromBucket(key: string) {
|
|
264
|
+
return await this.bucketService.delete(key);
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
```
|
|
268
|
+
|
|
269
|
+
### Firebase Service
|
|
270
|
+
|
|
271
|
+
The `FirebaseService` provides push notification functionality using Firebase Cloud Messaging (FCM).
|
|
272
|
+
|
|
273
|
+
#### Configuration
|
|
274
|
+
|
|
275
|
+
First, you need to set up Firebase Admin SDK with a service account key:
|
|
276
|
+
|
|
277
|
+
1. Download your Firebase service account key JSON file
|
|
278
|
+
2. Place it in your project root as `serviceAccount.json`
|
|
279
|
+
3. The service will automatically initialize Firebase Admin SDK
|
|
280
|
+
|
|
281
|
+
#### Interfaces
|
|
282
|
+
|
|
283
|
+
```typescript
|
|
284
|
+
import { INotification } from 'zync-nest-library';
|
|
285
|
+
|
|
286
|
+
interface INotification {
|
|
287
|
+
title: string;
|
|
288
|
+
body: string;
|
|
289
|
+
topic?: string;
|
|
290
|
+
image?: string;
|
|
291
|
+
data?: {
|
|
292
|
+
type: "ORDER" | "SCHEME" | "RATE" | "PUSH" | "FEED";
|
|
293
|
+
[x: string]: string;
|
|
294
|
+
};
|
|
295
|
+
}
|
|
296
|
+
```
|
|
297
|
+
|
|
298
|
+
#### Usage
|
|
299
|
+
|
|
300
|
+
```typescript
|
|
301
|
+
import { Injectable } from '@nestjs/common';
|
|
302
|
+
import { FirebaseService, INotification } from 'zync-nest-library';
|
|
303
|
+
|
|
304
|
+
@Injectable()
|
|
305
|
+
export class NotificationService {
|
|
306
|
+
constructor(private readonly firebaseService: FirebaseService) {}
|
|
307
|
+
|
|
308
|
+
async sendToDevices(tokens: string[], notification: INotification) {
|
|
309
|
+
await this.firebaseService.sendPushNotification(tokens, notification);
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
async sendToTopic(topic: string, notification: INotification) {
|
|
313
|
+
await this.firebaseService.sendPushNotificationToTopic(topic, notification);
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
async subscribeToTopic(topic: string, tokens: string[]) {
|
|
317
|
+
await this.firebaseService.subscribeToTopic(topic, tokens);
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
```
|
|
321
|
+
|
|
322
|
+
### Mailer Service
|
|
323
|
+
|
|
324
|
+
The `MailerService` provides email sending functionality with template support.
|
|
325
|
+
|
|
326
|
+
#### Configuration
|
|
327
|
+
|
|
328
|
+
```typescript
|
|
329
|
+
import { IMailerConfig } from 'zync-nest-library';
|
|
330
|
+
|
|
331
|
+
const mailerConfig: IMailerConfig = {
|
|
332
|
+
logo: 'https://example.com/logo.png',
|
|
333
|
+
host: 'smtp.gmail.com',
|
|
334
|
+
user: 'your-email@gmail.com',
|
|
335
|
+
pass: 'your-app-password',
|
|
336
|
+
from: 'noreply@yourcompany.com',
|
|
337
|
+
port: '587',
|
|
338
|
+
playStore: 'https://play.google.com/store/apps/details?id=your.app',
|
|
339
|
+
appStore: 'https://apps.apple.com/app/your-app/id123456789'
|
|
340
|
+
};
|
|
341
|
+
```
|
|
342
|
+
|
|
343
|
+
#### Environment Variables
|
|
344
|
+
|
|
345
|
+
```env
|
|
346
|
+
# Email Configuration
|
|
347
|
+
MAILER_HOST=smtp.gmail.com
|
|
348
|
+
MAILER_USER=your-email@gmail.com
|
|
349
|
+
MAILER_PASS=your-app-password
|
|
350
|
+
MAILER_FROM=noreply@yourcompany.com
|
|
351
|
+
MAILER_PORT=587
|
|
352
|
+
MAILER_LOGO=https://example.com/logo.png
|
|
353
|
+
MAILER_PLAY_STORE=https://play.google.com/store/apps/details?id=your.app
|
|
354
|
+
MAILER_APP_STORE=https://apps.apple.com/app/your-app/id123456789
|
|
355
|
+
```
|
|
356
|
+
|
|
357
|
+
#### Usage
|
|
358
|
+
|
|
359
|
+
```typescript
|
|
360
|
+
import { Injectable } from '@nestjs/common';
|
|
361
|
+
import { MailerService, IMail } from 'zync-nest-library';
|
|
362
|
+
|
|
363
|
+
@Injectable()
|
|
364
|
+
export class EmailService {
|
|
365
|
+
constructor(private readonly mailerService: MailerService) {}
|
|
366
|
+
|
|
367
|
+
async sendEmail(mailData: IMail) {
|
|
368
|
+
await this.mailerService.sendMail(mailData);
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
async sendWelcomeEmail(email: string, name: string) {
|
|
372
|
+
const mailData: IMail = {
|
|
373
|
+
to: email,
|
|
374
|
+
subject: 'Welcome to Our Platform',
|
|
375
|
+
template: 'welcome',
|
|
376
|
+
context: {
|
|
377
|
+
name: name,
|
|
378
|
+
loginUrl: 'https://yourapp.com/login'
|
|
379
|
+
}
|
|
380
|
+
};
|
|
381
|
+
|
|
382
|
+
await this.mailerService.sendMail(mailData);
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
```
|
|
386
|
+
|
|
387
|
+
### Exabytes SMS Service
|
|
388
|
+
|
|
389
|
+
The Exabytes SMS service provides SMS sending functionality via Exabytes API.
|
|
390
|
+
|
|
391
|
+
#### Configuration
|
|
392
|
+
|
|
393
|
+
Configure the Exabytes SMS service using environment variables:
|
|
394
|
+
|
|
395
|
+
```env
|
|
396
|
+
# Exabytes SMS Configuration
|
|
397
|
+
exabytes_sms_username=your_username
|
|
398
|
+
exabytes_sms_password=your_password
|
|
399
|
+
exabytes_sms_url=https://sms.exabytes.com/api/send
|
|
400
|
+
exabytes_sms_sendid=YOUR_SENDER_ID
|
|
401
|
+
```
|
|
402
|
+
|
|
403
|
+
#### Usage
|
|
404
|
+
|
|
405
|
+
The Exabytes SMS service is typically used through the unified Message Service for SMS functionality.
|
|
406
|
+
|
|
407
|
+
### UltraMsg Service
|
|
408
|
+
|
|
409
|
+
The `UltraMsgService` provides WhatsApp messaging functionality via UltraMsg API.
|
|
410
|
+
|
|
411
|
+
#### Configuration
|
|
412
|
+
|
|
413
|
+
Configure the UltraMsg service using environment variables:
|
|
414
|
+
|
|
415
|
+
```env
|
|
416
|
+
# UltraMsg Configuration
|
|
417
|
+
ultramsg_instance=your_instance_id
|
|
418
|
+
ultramsg_token=your_api_token
|
|
419
|
+
ultramsg_url=https://api.ultramsg.com
|
|
420
|
+
```
|
|
421
|
+
|
|
422
|
+
#### Usage
|
|
423
|
+
|
|
424
|
+
```typescript
|
|
425
|
+
import { Injectable } from '@nestjs/common';
|
|
426
|
+
import { UltraMsgService } from 'zync-nest-library';
|
|
427
|
+
|
|
428
|
+
@Injectable()
|
|
429
|
+
export class WhatsAppService {
|
|
430
|
+
constructor(private readonly ultraMsgService: UltraMsgService) {}
|
|
431
|
+
|
|
432
|
+
async sendMessage(phone: string, message: string) {
|
|
433
|
+
return await this.ultraMsgService.sendMessage(phone, message);
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
async sendDocument(phone: string, document: string, caption?: string) {
|
|
437
|
+
return await this.ultraMsgService.sendDocument(phone, document, caption);
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
```
|
|
441
|
+
|
|
442
|
+
### Message Service
|
|
443
|
+
|
|
444
|
+
The `MessageService` provides a unified interface for sending messages across different channels (Email, SMS, WhatsApp, Push Notifications).
|
|
445
|
+
|
|
446
|
+
#### Usage
|
|
447
|
+
|
|
448
|
+
```typescript
|
|
449
|
+
import { Injectable } from '@nestjs/common';
|
|
450
|
+
import { MessageService } from 'zync-nest-library';
|
|
451
|
+
|
|
452
|
+
@Injectable()
|
|
453
|
+
export class UnifiedMessageService {
|
|
454
|
+
constructor(private readonly messageService: MessageService) {}
|
|
455
|
+
|
|
456
|
+
async sendMultiChannelMessage(
|
|
457
|
+
email: string,
|
|
458
|
+
phone: string,
|
|
459
|
+
pushTokens: string[],
|
|
460
|
+
message: any
|
|
461
|
+
) {
|
|
462
|
+
// Send via email
|
|
463
|
+
await this.messageService.sendEmail({
|
|
464
|
+
to: email,
|
|
465
|
+
subject: message.subject,
|
|
466
|
+
template: message.template,
|
|
467
|
+
context: message.context
|
|
468
|
+
});
|
|
469
|
+
|
|
470
|
+
// Send via SMS (if configured)
|
|
471
|
+
// await this.messageService.sendSMS(phone, message.text);
|
|
472
|
+
|
|
473
|
+
// Send via WhatsApp (if configured)
|
|
474
|
+
// await this.messageService.sendWhatsApp(phone, message.text);
|
|
475
|
+
|
|
476
|
+
// Send push notification
|
|
477
|
+
await this.messageService.sendPushNotification(pushTokens, {
|
|
478
|
+
title: message.title,
|
|
479
|
+
body: message.body,
|
|
480
|
+
data: message.data
|
|
481
|
+
});
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
```
|
|
485
|
+
|
|
486
|
+
### Utility Functions
|
|
487
|
+
|
|
488
|
+
The library includes various utility functions for common operations:
|
|
489
|
+
|
|
490
|
+
```typescript
|
|
491
|
+
import {
|
|
492
|
+
DateUtils,
|
|
493
|
+
mkdir,
|
|
494
|
+
uuidFilenameTransform,
|
|
495
|
+
bufferFromBase64,
|
|
496
|
+
getBase64FileName,
|
|
497
|
+
toSlug,
|
|
498
|
+
fileDownloadResponse
|
|
499
|
+
} from 'zync-nest-library';
|
|
500
|
+
|
|
501
|
+
// Date utilities
|
|
502
|
+
const timestamp = DateUtils.formatStringDate('25/12/2023');
|
|
503
|
+
|
|
504
|
+
// File utilities
|
|
505
|
+
const uniqueFilename = uuidFilenameTransform('image.jpg'); // Returns: uuid.jpg
|
|
506
|
+
const buffer = bufferFromBase64('data:image/jpeg;base64,...');
|
|
507
|
+
const filename = getBase64FileName('data:image/jpeg;base64,...'); // Returns: uuid.jpeg
|
|
508
|
+
|
|
509
|
+
// String utilities
|
|
510
|
+
const slug = toSlug('My Product Title'); // Returns: my-product-title-abc123
|
|
511
|
+
|
|
512
|
+
// Directory creation
|
|
513
|
+
mkdir('./uploads/images');
|
|
514
|
+
|
|
515
|
+
// File download response (Express)
|
|
516
|
+
fileDownloadResponse(res, buffer, 'report', 'pdf');
|
|
517
|
+
```
|
|
518
|
+
|
|
519
|
+
## 🔧 Configuration
|
|
520
|
+
|
|
521
|
+
### Environment Variables
|
|
522
|
+
|
|
523
|
+
```env
|
|
524
|
+
# Cloud Storage (DigitalOcean Spaces / AWS S3)
|
|
525
|
+
#-----------------s3-----------------#
|
|
526
|
+
aws_access_key_id=xxxx
|
|
527
|
+
aws_secret_access_key=xxxxx
|
|
528
|
+
aws_s3_region=nyc3
|
|
529
|
+
aws_s3_endpoint=https://xxxx.nyc3.digitaloceanspaces.com
|
|
530
|
+
aws_bucket=xxxx
|
|
531
|
+
aws_base_key=development
|
|
532
|
+
|
|
533
|
+
# Email Configuration (Mailer Service)
|
|
534
|
+
#-----------------mailer-----------------#
|
|
535
|
+
MAILER_HOST=smtp.gmail.com
|
|
536
|
+
MAILER_USER=your-email@gmail.com
|
|
537
|
+
MAILER_PASS=your-app-password
|
|
538
|
+
MAILER_FROM=noreply@yourcompany.com
|
|
539
|
+
MAILER_PORT=587
|
|
540
|
+
MAILER_LOGO=https://example.com/logo.png
|
|
541
|
+
MAILER_PLAY_STORE=https://play.google.com/store/apps/details?id=your.app
|
|
542
|
+
MAILER_APP_STORE=https://apps.apple.com/app/your-app/id123456789
|
|
543
|
+
|
|
544
|
+
# Exabytes SMS Configuration
|
|
545
|
+
#-----------------exabytes-----------------#
|
|
546
|
+
exabytes_sms_username=your_username
|
|
547
|
+
exabytes_sms_password=your_password
|
|
548
|
+
exabytes_sms_url=https://sms.exabytes.com/api/send
|
|
549
|
+
exabytes_sms_sendid=YOUR_SENDER_ID
|
|
550
|
+
|
|
551
|
+
# UltraMsg WhatsApp Configuration
|
|
552
|
+
#-----------------ultramsg-----------------#
|
|
553
|
+
ultramsg_instance=your_instance_id
|
|
554
|
+
ultramsg_token=your_api_token
|
|
555
|
+
ultramsg_url=https://api.ultramsg.com
|
|
556
|
+
|
|
557
|
+
# Firebase Configuration
|
|
558
|
+
#-----------------firebase-----------------#
|
|
559
|
+
# Note: Firebase requires a service account JSON file
|
|
560
|
+
# Place your serviceAccount.json file in the project root
|
|
561
|
+
# Download from: Firebase Console > Project Settings > Service Accounts
|
|
562
|
+
```
|
|
563
|
+
|
|
564
|
+
## 🛠️ Development
|
|
565
|
+
|
|
566
|
+
### Scripts
|
|
567
|
+
|
|
568
|
+
```bash
|
|
569
|
+
# Build the library
|
|
570
|
+
pnpm run build
|
|
571
|
+
|
|
572
|
+
# Build in watch mode
|
|
573
|
+
pnpm run build:watch
|
|
574
|
+
|
|
575
|
+
# Clean build artifacts
|
|
576
|
+
pnpm run clean
|
|
577
|
+
|
|
578
|
+
# Build and clean
|
|
579
|
+
pnpm run clean && pnpm run build
|
|
580
|
+
```
|
|
581
|
+
|
|
582
|
+
### Project Structure
|
|
583
|
+
|
|
584
|
+
```
|
|
585
|
+
libs/
|
|
586
|
+
├── src/
|
|
587
|
+
│ ├── dbbackup/ # Database backup functionality
|
|
588
|
+
│ │ ├── backup.config.ts
|
|
589
|
+
│ │ ├── backup.interface.ts
|
|
590
|
+
│ │ ├── backup.module.ts
|
|
591
|
+
│ │ └── backup.service.ts
|
|
592
|
+
│ ├── upload/ # File upload functionality
|
|
593
|
+
│ │ ├── bucket/ # Cloud storage integration
|
|
594
|
+
│ │ ├── upload.interface.ts
|
|
595
|
+
│ │ ├── upload.module.ts
|
|
596
|
+
│ │ ├── upload.service.ts
|
|
597
|
+
│ │ └── upload.utils.ts
|
|
598
|
+
│ ├── firebase/ # Firebase push notifications
|
|
599
|
+
│ │ ├── firebase.interface.ts
|
|
600
|
+
│ │ ├── firebase.module.ts
|
|
601
|
+
│ │ └── firebase.service.ts
|
|
602
|
+
│ ├── mailer/ # Email service
|
|
603
|
+
│ │ ├── mailer.interface.ts
|
|
604
|
+
│ │ ├── mailer.module.ts
|
|
605
|
+
│ │ └── mailer.service.ts
|
|
606
|
+
│ ├── message/ # Unified messaging service
|
|
607
|
+
│ │ ├── message.module.ts
|
|
608
|
+
│ │ └── message.service.ts
|
|
609
|
+
│ ├── exabytes/ # SMS service via Exabytes
|
|
610
|
+
│ │ ├── exabytes.config.ts
|
|
611
|
+
│ │ ├── exabytes.interface.ts
|
|
612
|
+
│ │ ├── exabytes.module.ts
|
|
613
|
+
│ │ └── exabytes.service.ts
|
|
614
|
+
│ ├── ultramsg/ # WhatsApp service via UltraMsg
|
|
615
|
+
│ │ ├── ultramsg.config.ts
|
|
616
|
+
│ │ ├── ultramsg.interface.ts
|
|
617
|
+
│ │ ├── ultramsg.module.ts
|
|
618
|
+
│ │ └── ultramsg.service.ts
|
|
619
|
+
│ ├── utils/ # Utility functions
|
|
620
|
+
│ │ ├── date.ts
|
|
621
|
+
│ │ ├── object.ts
|
|
622
|
+
│ │ └── index.ts
|
|
623
|
+
│ └── index.ts # Main exports
|
|
624
|
+
└── tsconfig.lib.json
|
|
625
|
+
```
|
|
626
|
+
### Common Issues
|
|
627
|
+
|
|
628
|
+
#### 1. pnpm link not working
|
|
629
|
+
If `pnpm link` doesn't work, use the file path dependency method instead:
|
|
630
|
+
|
|
631
|
+
```json
|
|
632
|
+
{
|
|
633
|
+
"dependencies": {
|
|
634
|
+
"zync-nest-library": "file:../../zync-library/zync-nest-library"
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
```
|
|
638
|
+
|
|
639
|
+
#### 2. Peer dependency warnings
|
|
640
|
+
If you see peer dependency warnings (e.g., `@nestjs/core` version mismatch), you can:
|
|
641
|
+
|
|
642
|
+
- Update your project's NestJS version to match
|
|
643
|
+
- Or add to your `package.json`:
|
|
644
|
+
```json
|
|
645
|
+
{
|
|
646
|
+
"pnpm": {
|
|
647
|
+
"peerDependencyRules": {
|
|
648
|
+
"ignoreMissing": ["@nestjs/core"]
|
|
649
|
+
}
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
```
|
|
653
|
+
|
|
654
|
+
#### 3. Build errors after linking
|
|
655
|
+
Make sure to build the library first:
|
|
656
|
+
```bash
|
|
657
|
+
cd zync-nest-library
|
|
658
|
+
pnpm run build
|
|
659
|
+
```
|
|
660
|
+
|
|
661
|
+
#### 4. Changes not reflected
|
|
662
|
+
After making changes to the library:
|
|
663
|
+
```bash
|
|
664
|
+
# In library directory
|
|
665
|
+
pnpm run build
|
|
666
|
+
|
|
667
|
+
# No need to reinstall - changes are automatically available in linked projects
|
|
668
|
+
```
|
|
669
|
+
|
|
670
|
+
## 🐛 Issues
|
|
671
|
+
|
|
672
|
+
If you encounter any issues or have questions, please file an issue on the project repository.
|