@tomei/media 0.4.0 → 0.4.1
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/.commitlintrc.json +22 -22
- package/.eslintrc.js +66 -66
- package/.husky/commit-msg +4 -4
- package/.husky/pre-commit +4 -4
- package/.prettierrc +4 -4
- package/.prettierrc copy +3 -3
- package/README.md +93 -93
- package/dist/base/base.medias.d.ts +38 -38
- package/dist/base/base.medias.js +169 -169
- package/dist/base/base.repository.abstract.d.ts +12 -12
- package/dist/base/base.repository.abstract.js +21 -21
- package/dist/base/base.repository.interface.d.ts +9 -9
- package/dist/base/base.repository.interface.js +2 -2
- package/dist/base/index.d.ts +2 -0
- package/dist/base/index.js +6 -0
- package/dist/base/index.js.map +1 -0
- package/dist/common/common.module.d.ts +2 -2
- package/dist/common/common.module.js +22 -22
- package/dist/common/common.service.d.ts +13 -13
- package/dist/common/common.service.js +107 -107
- package/dist/common/common.service.spec.d.ts +0 -0
- package/dist/common/common.service.spec.js +5 -0
- package/dist/common/common.service.spec.js.map +1 -0
- package/dist/common/dto/add-field-translation.dto.d.ts +7 -7
- package/dist/common/dto/add-field-translation.dto.js +41 -41
- package/dist/common/dto/get-media.dto.d.ts +5 -5
- package/dist/common/dto/get-media.dto.js +28 -28
- package/dist/common/dto/get-media.dto.js.map +1 -1
- package/dist/dto/external-media.dto.d.ts +10 -10
- package/dist/dto/external-media.dto.js +69 -69
- package/dist/dto/internal-medias.dto.d.ts +12 -12
- package/dist/dto/internal-medias.dto.js +86 -86
- package/dist/dto/medias.dto.d.ts +12 -0
- package/dist/dto/medias.dto.js +88 -0
- package/dist/dto/medias.dto.js.map +1 -0
- package/dist/entities/index.d.ts +2 -0
- package/dist/entities/index.js +6 -0
- package/dist/entities/index.js.map +1 -0
- package/dist/entities/media.entity.d.ts +21 -0
- package/dist/entities/media.entity.js +152 -0
- package/dist/entities/media.entity.js.map +1 -0
- package/dist/entities/medias.entity.d.ts +21 -21
- package/dist/entities/medias.entity.js +151 -151
- package/dist/enum/index.d.ts +2 -2
- package/dist/enum/index.js +5 -5
- package/dist/enum/medias.enum.d.ts +5 -5
- package/dist/enum/medias.enum.js +9 -9
- package/dist/helpers/error.d.ts +1 -1
- package/dist/helpers/error.js +13 -13
- package/dist/helpers/index.d.ts +2 -2
- package/dist/helpers/index.js +5 -5
- package/dist/index.d.ts +11 -11
- package/dist/index.js +19 -19
- package/dist/interfaces/base.medias-attr.interface.d.ts +20 -20
- package/dist/interfaces/base.medias-attr.interface.js +2 -2
- package/dist/interfaces/commonService.interface.d.ts +17 -0
- package/dist/interfaces/commonService.interface.js +3 -0
- package/dist/interfaces/commonService.interface.js.map +1 -0
- package/dist/interfaces/index.d.ts +3 -0
- package/dist/interfaces/index.js +3 -0
- package/dist/interfaces/index.js.map +1 -0
- package/dist/interfaces/media.repository.interface.d.ts +10 -0
- package/dist/interfaces/media.repository.interface.js +3 -0
- package/dist/interfaces/media.repository.interface.js.map +1 -0
- package/dist/interfaces/medias-attr.interface.d.ts +22 -0
- package/dist/interfaces/medias-attr.interface.js +3 -0
- package/dist/interfaces/medias-attr.interface.js.map +1 -0
- package/dist/interfaces/medias.repository.interface.d.ts +3 -3
- package/dist/interfaces/medias.repository.interface.js +2 -2
- package/dist/medias.controller copy.d.ts +25 -0
- package/dist/medias.controller copy.js +225 -0
- package/dist/medias.controller copy.js.map +1 -0
- package/dist/medias.controller.d.ts +29 -25
- package/dist/medias.controller.js +119 -224
- package/dist/medias.controller.js.map +1 -1
- package/dist/medias.controller.old.d.ts +25 -0
- package/dist/medias.controller.old.js +225 -0
- package/dist/medias.controller.old.js.map +1 -0
- package/dist/medias.d.ts +62 -62
- package/dist/medias.js +507 -507
- package/dist/medias.module.d.ts +2 -2
- package/dist/medias.module.js +31 -34
- package/dist/medias.module.js.map +1 -1
- package/dist/medias.repository.d.ts +16 -16
- package/dist/medias.repository.js +46 -46
- package/dist/pipe/append-id.pipe.d.ts +6 -6
- package/dist/pipe/append-id.pipe.js +27 -27
- package/dist/pipe/validate-enum.pipe.d.ts +6 -6
- package/dist/pipe/validate-enum.pipe.js +33 -33
- package/dist/pipe/validate-id.pipe.d.ts +6 -6
- package/dist/pipe/validate-id.pipe.js +30 -30
- package/dist/pipe/validate-search.pipe.d.ts +7 -7
- package/dist/pipe/validate-search.pipe.js +45 -45
- package/dist/responses/deleted.response.d.ts +3 -3
- package/dist/responses/deleted.response.js +20 -20
- package/dist/responses/pagination.response.d.ts +5 -5
- package/dist/responses/pagination.response.js +25 -25
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/migration/media-migration.js +82 -82
- package/package.json +72 -72
- package/src/base/base.medias.ts +209 -209
- package/src/common/common.module.ts +10 -10
- package/src/common/common.service.ts +109 -109
- package/src/common/dto/add-field-translation.dto.ts +23 -23
- package/src/common/dto/get-media.dto.ts +12 -12
- package/src/index.ts +22 -22
- package/src/medias.repository.ts +41 -41
- package/src/medias.ts +688 -688
- package/src/pipe/validate-search.pipe.ts +44 -44
- package/tsconfig.build.json +4 -4
- package/tslint.json +18 -18
package/src/base/base.medias.ts
CHANGED
|
@@ -1,209 +1,209 @@
|
|
|
1
|
-
import { MediaType } from '../enum/medias.enum';
|
|
2
|
-
import * as cuid from 'cuid';
|
|
3
|
-
import { BadRequestException } from '@nestjs/common';
|
|
4
|
-
import { IMediasRepository } from '../interfaces/medias.repository.interface';
|
|
5
|
-
import { MediasModel } from '../entities/medias.entity';
|
|
6
|
-
import { IBaseMediasAttr } from '../interfaces/base.medias-attr.interface';
|
|
7
|
-
|
|
8
|
-
export abstract class BaseMedias {
|
|
9
|
-
MediaId: string;
|
|
10
|
-
ObjectId: string;
|
|
11
|
-
ObjectType: string;
|
|
12
|
-
Title: string;
|
|
13
|
-
Description: string;
|
|
14
|
-
Type: MediaType;
|
|
15
|
-
IsExternalYN: string;
|
|
16
|
-
ExternalSource: string;
|
|
17
|
-
IsEncryptedYN: string;
|
|
18
|
-
FileName: string;
|
|
19
|
-
FileExtension: string;
|
|
20
|
-
FilePath: string;
|
|
21
|
-
URL: string;
|
|
22
|
-
CreatedById: string;
|
|
23
|
-
CreatedAt: Date;
|
|
24
|
-
UpdatedById: string;
|
|
25
|
-
UpdatedAt: Date;
|
|
26
|
-
mediasRepository: IMediasRepository;
|
|
27
|
-
container: string;
|
|
28
|
-
|
|
29
|
-
protected constructor(
|
|
30
|
-
mediasRepository: IMediasRepository,
|
|
31
|
-
media?: IBaseMediasAttr,
|
|
32
|
-
) {
|
|
33
|
-
this.mediasRepository = mediasRepository;
|
|
34
|
-
if (media) {
|
|
35
|
-
this.init(media);
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
init(media: IBaseMediasAttr){
|
|
40
|
-
this.MediaId = media.MediaId ? media.MediaId : cuid();
|
|
41
|
-
this.ObjectId = media.ObjectId;
|
|
42
|
-
this.ObjectType = media.ObjectType;
|
|
43
|
-
this.Title = media.Title;
|
|
44
|
-
this.Description = media.Description;
|
|
45
|
-
this.Type = media.Type;
|
|
46
|
-
this.IsExternalYN = media.IsExternalYN;
|
|
47
|
-
this.ExternalSource = media.ExternalSource;
|
|
48
|
-
this.IsEncryptedYN = media.IsEncryptedYN;
|
|
49
|
-
this.FileName = media.FileName;
|
|
50
|
-
this.FileExtension = media.FileExtension;
|
|
51
|
-
this.URL =
|
|
52
|
-
media.URL && media.IsExternalYN === 'Y'
|
|
53
|
-
? media.URL
|
|
54
|
-
: this.createSaveLocation();
|
|
55
|
-
this.FilePath =
|
|
56
|
-
media.FilePath && media.IsExternalYN === 'Y'
|
|
57
|
-
? media.FilePath
|
|
58
|
-
: this.createFilePath();
|
|
59
|
-
this.CreatedById = media.CreatedById;
|
|
60
|
-
this.CreatedAt = media.CreatedAt;
|
|
61
|
-
this.UpdatedById = media.UpdatedById;
|
|
62
|
-
this.UpdatedAt = media.UpdatedAt;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
createSaveLocation(): string {
|
|
66
|
-
try {
|
|
67
|
-
const mediaFileStorageType = process.env.MEDIA_STORAGE_TYPE;
|
|
68
|
-
let basePath: string;
|
|
69
|
-
if (mediaFileStorageType === 'local') {
|
|
70
|
-
basePath = process.env.MEDIA_LOCAL_STORAGE_PATH;
|
|
71
|
-
} else {
|
|
72
|
-
basePath = process.env.MEDIA_AZUREBLOB_CONTAINER_NAME;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
if (!basePath) {
|
|
76
|
-
throw new BadRequestException(
|
|
77
|
-
'MEDIA_LOCAL_STORAGE_PATH and or MEDIA_AZUREBLOB_CONTAINER_NAME not found.',
|
|
78
|
-
);
|
|
79
|
-
}
|
|
80
|
-
return basePath + `/${this.ObjectType}/${this.ObjectId}`;
|
|
81
|
-
} catch (error) {
|
|
82
|
-
throw error;
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
createFilePath(): string {
|
|
87
|
-
try {
|
|
88
|
-
const fileLocation = this.createSaveLocation();
|
|
89
|
-
return `${fileLocation}/${this.FileName}.${this.FileExtension}`;
|
|
90
|
-
} catch (error) {
|
|
91
|
-
throw error;
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
async create(options?: any): Promise<MediasModel> {
|
|
96
|
-
try {
|
|
97
|
-
return this.mediasRepository.create(
|
|
98
|
-
{
|
|
99
|
-
MediaId: this.MediaId,
|
|
100
|
-
ObjectId: this.ObjectId,
|
|
101
|
-
ObjectType: this.ObjectType,
|
|
102
|
-
Title: this.Title,
|
|
103
|
-
Description: this.Description,
|
|
104
|
-
Type: this.Type,
|
|
105
|
-
IsExternalYN: this.IsExternalYN,
|
|
106
|
-
ExternalSource: this.ExternalSource,
|
|
107
|
-
IsEncryptedYN: this.IsEncryptedYN,
|
|
108
|
-
FileName: this.FileName,
|
|
109
|
-
FileExtension: this.FileExtension,
|
|
110
|
-
FilePath: this.FilePath,
|
|
111
|
-
URL: this.URL,
|
|
112
|
-
CreatedById: this.CreatedById,
|
|
113
|
-
CreatedAt: this.CreatedAt,
|
|
114
|
-
UpdatedById: this.UpdatedById,
|
|
115
|
-
UpdatedAt: this.UpdatedAt,
|
|
116
|
-
},
|
|
117
|
-
options,
|
|
118
|
-
);
|
|
119
|
-
} catch (error) {
|
|
120
|
-
throw error;
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
async update(options?: any): Promise<MediasModel | any> {
|
|
125
|
-
try {
|
|
126
|
-
const media = await this.mediasRepository.findOne({
|
|
127
|
-
where: {
|
|
128
|
-
MediaId: this.MediaId,
|
|
129
|
-
},
|
|
130
|
-
});
|
|
131
|
-
|
|
132
|
-
if (!media) {
|
|
133
|
-
throw new BadRequestException('Media not found.');
|
|
134
|
-
}
|
|
135
|
-
return await media.update(
|
|
136
|
-
{
|
|
137
|
-
MediaId: this.MediaId,
|
|
138
|
-
ObjectId: this.ObjectId,
|
|
139
|
-
ObjectType: this.ObjectType,
|
|
140
|
-
Title: this.Title,
|
|
141
|
-
Description: this.Description,
|
|
142
|
-
Type: this.Type,
|
|
143
|
-
IsExternalYN: this.IsExternalYN,
|
|
144
|
-
ExternalSource: this.ExternalSource,
|
|
145
|
-
IsEncryptedYN: this.IsEncryptedYN,
|
|
146
|
-
FileName: this.FileName,
|
|
147
|
-
FileExtension: this.FileExtension,
|
|
148
|
-
FilePath: this.FilePath,
|
|
149
|
-
URL: this.URL,
|
|
150
|
-
CreatedById: this.CreatedById,
|
|
151
|
-
CreatedAt: this.CreatedAt,
|
|
152
|
-
UpdatedById: this.UpdatedById,
|
|
153
|
-
UpdatedAt: this.UpdatedAt,
|
|
154
|
-
},
|
|
155
|
-
options,
|
|
156
|
-
);
|
|
157
|
-
} catch (error) {
|
|
158
|
-
throw error;
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
async delete(options?: any): Promise<MediasModel> {
|
|
163
|
-
try {
|
|
164
|
-
const media = await this.mediasRepository.findOne({
|
|
165
|
-
where: {
|
|
166
|
-
MediaId: this.MediaId,
|
|
167
|
-
},
|
|
168
|
-
});
|
|
169
|
-
|
|
170
|
-
const data = {
|
|
171
|
-
...media.get({ plain: true }),
|
|
172
|
-
};
|
|
173
|
-
|
|
174
|
-
if (!media) {
|
|
175
|
-
throw new BadRequestException('Media not found.');
|
|
176
|
-
}
|
|
177
|
-
await media.destroy(options);
|
|
178
|
-
return data;
|
|
179
|
-
} catch (error) {
|
|
180
|
-
throw error;
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
async findAll(options?: any): Promise<MediasModel[]> {
|
|
185
|
-
try {
|
|
186
|
-
return this.mediasRepository.findAll(options);
|
|
187
|
-
} catch (error) {
|
|
188
|
-
throw error;
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
async findAllWithPagination(
|
|
193
|
-
options?: any,
|
|
194
|
-
): Promise<{ count: number; rows: MediasModel[] }> {
|
|
195
|
-
try {
|
|
196
|
-
return this.mediasRepository.findAllWithPagination(options);
|
|
197
|
-
} catch (error) {
|
|
198
|
-
throw error;
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
async findOne(options?: any): Promise<MediasModel> {
|
|
203
|
-
try {
|
|
204
|
-
return this.mediasRepository.findOne(options);
|
|
205
|
-
} catch (error) {
|
|
206
|
-
throw error;
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
}
|
|
1
|
+
import { MediaType } from '../enum/medias.enum';
|
|
2
|
+
import * as cuid from 'cuid';
|
|
3
|
+
import { BadRequestException } from '@nestjs/common';
|
|
4
|
+
import { IMediasRepository } from '../interfaces/medias.repository.interface';
|
|
5
|
+
import { MediasModel } from '../entities/medias.entity';
|
|
6
|
+
import { IBaseMediasAttr } from '../interfaces/base.medias-attr.interface';
|
|
7
|
+
|
|
8
|
+
export abstract class BaseMedias {
|
|
9
|
+
MediaId: string;
|
|
10
|
+
ObjectId: string;
|
|
11
|
+
ObjectType: string;
|
|
12
|
+
Title: string;
|
|
13
|
+
Description: string;
|
|
14
|
+
Type: MediaType;
|
|
15
|
+
IsExternalYN: string;
|
|
16
|
+
ExternalSource: string;
|
|
17
|
+
IsEncryptedYN: string;
|
|
18
|
+
FileName: string;
|
|
19
|
+
FileExtension: string;
|
|
20
|
+
FilePath: string;
|
|
21
|
+
URL: string;
|
|
22
|
+
CreatedById: string;
|
|
23
|
+
CreatedAt: Date;
|
|
24
|
+
UpdatedById: string;
|
|
25
|
+
UpdatedAt: Date;
|
|
26
|
+
mediasRepository: IMediasRepository;
|
|
27
|
+
container: string;
|
|
28
|
+
|
|
29
|
+
protected constructor(
|
|
30
|
+
mediasRepository: IMediasRepository,
|
|
31
|
+
media?: IBaseMediasAttr,
|
|
32
|
+
) {
|
|
33
|
+
this.mediasRepository = mediasRepository;
|
|
34
|
+
if (media) {
|
|
35
|
+
this.init(media);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
init(media: IBaseMediasAttr){
|
|
40
|
+
this.MediaId = media.MediaId ? media.MediaId : cuid();
|
|
41
|
+
this.ObjectId = media.ObjectId;
|
|
42
|
+
this.ObjectType = media.ObjectType;
|
|
43
|
+
this.Title = media.Title;
|
|
44
|
+
this.Description = media.Description;
|
|
45
|
+
this.Type = media.Type;
|
|
46
|
+
this.IsExternalYN = media.IsExternalYN;
|
|
47
|
+
this.ExternalSource = media.ExternalSource;
|
|
48
|
+
this.IsEncryptedYN = media.IsEncryptedYN;
|
|
49
|
+
this.FileName = media.FileName;
|
|
50
|
+
this.FileExtension = media.FileExtension;
|
|
51
|
+
this.URL =
|
|
52
|
+
media.URL && media.IsExternalYN === 'Y'
|
|
53
|
+
? media.URL
|
|
54
|
+
: this.createSaveLocation();
|
|
55
|
+
this.FilePath =
|
|
56
|
+
media.FilePath && media.IsExternalYN === 'Y'
|
|
57
|
+
? media.FilePath
|
|
58
|
+
: this.createFilePath();
|
|
59
|
+
this.CreatedById = media.CreatedById;
|
|
60
|
+
this.CreatedAt = media.CreatedAt;
|
|
61
|
+
this.UpdatedById = media.UpdatedById;
|
|
62
|
+
this.UpdatedAt = media.UpdatedAt;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
createSaveLocation(): string {
|
|
66
|
+
try {
|
|
67
|
+
const mediaFileStorageType = process.env.MEDIA_STORAGE_TYPE;
|
|
68
|
+
let basePath: string;
|
|
69
|
+
if (mediaFileStorageType === 'local') {
|
|
70
|
+
basePath = process.env.MEDIA_LOCAL_STORAGE_PATH;
|
|
71
|
+
} else {
|
|
72
|
+
basePath = process.env.MEDIA_AZUREBLOB_CONTAINER_NAME;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (!basePath) {
|
|
76
|
+
throw new BadRequestException(
|
|
77
|
+
'MEDIA_LOCAL_STORAGE_PATH and or MEDIA_AZUREBLOB_CONTAINER_NAME not found.',
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
return basePath + `/${this.ObjectType}/${this.ObjectId}`;
|
|
81
|
+
} catch (error) {
|
|
82
|
+
throw error;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
createFilePath(): string {
|
|
87
|
+
try {
|
|
88
|
+
const fileLocation = this.createSaveLocation();
|
|
89
|
+
return `${fileLocation}/${this.FileName}.${this.FileExtension}`;
|
|
90
|
+
} catch (error) {
|
|
91
|
+
throw error;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
async create(options?: any): Promise<MediasModel> {
|
|
96
|
+
try {
|
|
97
|
+
return this.mediasRepository.create(
|
|
98
|
+
{
|
|
99
|
+
MediaId: this.MediaId,
|
|
100
|
+
ObjectId: this.ObjectId,
|
|
101
|
+
ObjectType: this.ObjectType,
|
|
102
|
+
Title: this.Title,
|
|
103
|
+
Description: this.Description,
|
|
104
|
+
Type: this.Type,
|
|
105
|
+
IsExternalYN: this.IsExternalYN,
|
|
106
|
+
ExternalSource: this.ExternalSource,
|
|
107
|
+
IsEncryptedYN: this.IsEncryptedYN,
|
|
108
|
+
FileName: this.FileName,
|
|
109
|
+
FileExtension: this.FileExtension,
|
|
110
|
+
FilePath: this.FilePath,
|
|
111
|
+
URL: this.URL,
|
|
112
|
+
CreatedById: this.CreatedById,
|
|
113
|
+
CreatedAt: this.CreatedAt,
|
|
114
|
+
UpdatedById: this.UpdatedById,
|
|
115
|
+
UpdatedAt: this.UpdatedAt,
|
|
116
|
+
},
|
|
117
|
+
options,
|
|
118
|
+
);
|
|
119
|
+
} catch (error) {
|
|
120
|
+
throw error;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
async update(options?: any): Promise<MediasModel | any> {
|
|
125
|
+
try {
|
|
126
|
+
const media = await this.mediasRepository.findOne({
|
|
127
|
+
where: {
|
|
128
|
+
MediaId: this.MediaId,
|
|
129
|
+
},
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
if (!media) {
|
|
133
|
+
throw new BadRequestException('Media not found.');
|
|
134
|
+
}
|
|
135
|
+
return await media.update(
|
|
136
|
+
{
|
|
137
|
+
MediaId: this.MediaId,
|
|
138
|
+
ObjectId: this.ObjectId,
|
|
139
|
+
ObjectType: this.ObjectType,
|
|
140
|
+
Title: this.Title,
|
|
141
|
+
Description: this.Description,
|
|
142
|
+
Type: this.Type,
|
|
143
|
+
IsExternalYN: this.IsExternalYN,
|
|
144
|
+
ExternalSource: this.ExternalSource,
|
|
145
|
+
IsEncryptedYN: this.IsEncryptedYN,
|
|
146
|
+
FileName: this.FileName,
|
|
147
|
+
FileExtension: this.FileExtension,
|
|
148
|
+
FilePath: this.FilePath,
|
|
149
|
+
URL: this.URL,
|
|
150
|
+
CreatedById: this.CreatedById,
|
|
151
|
+
CreatedAt: this.CreatedAt,
|
|
152
|
+
UpdatedById: this.UpdatedById,
|
|
153
|
+
UpdatedAt: this.UpdatedAt,
|
|
154
|
+
},
|
|
155
|
+
options,
|
|
156
|
+
);
|
|
157
|
+
} catch (error) {
|
|
158
|
+
throw error;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
async delete(options?: any): Promise<MediasModel> {
|
|
163
|
+
try {
|
|
164
|
+
const media = await this.mediasRepository.findOne({
|
|
165
|
+
where: {
|
|
166
|
+
MediaId: this.MediaId,
|
|
167
|
+
},
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
const data = {
|
|
171
|
+
...media.get({ plain: true }),
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
if (!media) {
|
|
175
|
+
throw new BadRequestException('Media not found.');
|
|
176
|
+
}
|
|
177
|
+
await media.destroy(options);
|
|
178
|
+
return data;
|
|
179
|
+
} catch (error) {
|
|
180
|
+
throw error;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
async findAll(options?: any): Promise<MediasModel[]> {
|
|
185
|
+
try {
|
|
186
|
+
return this.mediasRepository.findAll(options);
|
|
187
|
+
} catch (error) {
|
|
188
|
+
throw error;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
async findAllWithPagination(
|
|
193
|
+
options?: any,
|
|
194
|
+
): Promise<{ count: number; rows: MediasModel[] }> {
|
|
195
|
+
try {
|
|
196
|
+
return this.mediasRepository.findAllWithPagination(options);
|
|
197
|
+
} catch (error) {
|
|
198
|
+
throw error;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
async findOne(options?: any): Promise<MediasModel> {
|
|
203
|
+
try {
|
|
204
|
+
return this.mediasRepository.findOne(options);
|
|
205
|
+
} catch (error) {
|
|
206
|
+
throw error;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
}
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { Module } from '@nestjs/common';
|
|
2
|
-
import { ConfigModule } from '@nestjs/config';
|
|
3
|
-
import { CommonService } from './common.service';
|
|
4
|
-
|
|
5
|
-
@Module({
|
|
6
|
-
providers: [CommonService],
|
|
7
|
-
imports: [ConfigModule],
|
|
8
|
-
exports: [CommonService],
|
|
9
|
-
})
|
|
10
|
-
export class CommonModule {}
|
|
1
|
+
import { Module } from '@nestjs/common';
|
|
2
|
+
import { ConfigModule } from '@nestjs/config';
|
|
3
|
+
import { CommonService } from './common.service';
|
|
4
|
+
|
|
5
|
+
@Module({
|
|
6
|
+
providers: [CommonService],
|
|
7
|
+
imports: [ConfigModule],
|
|
8
|
+
exports: [CommonService],
|
|
9
|
+
})
|
|
10
|
+
export class CommonModule {}
|
|
@@ -1,109 +1,109 @@
|
|
|
1
|
-
import {
|
|
2
|
-
HttpException,
|
|
3
|
-
Injectable,
|
|
4
|
-
InternalServerErrorException,
|
|
5
|
-
} from '@nestjs/common';
|
|
6
|
-
import { ConfigService } from '@nestjs/config';
|
|
7
|
-
import axios from 'axios';
|
|
8
|
-
import * as cuid from 'cuid';
|
|
9
|
-
import { omit, omitBy, keys, pick } from 'lodash';
|
|
10
|
-
import { AddFieldTranslationDto } from './dto/add-field-translation.dto';
|
|
11
|
-
import { GetMediaDto } from './dto/get-media.dto';
|
|
12
|
-
@Injectable()
|
|
13
|
-
export class CommonService {
|
|
14
|
-
private readonly commonApiUrl: string;
|
|
15
|
-
|
|
16
|
-
constructor(private configService: ConfigService) {
|
|
17
|
-
this.commonApiUrl = this.configService.get('COMMON_API_URL');
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
handleAxiosError(err) {
|
|
21
|
-
if (err.response) {
|
|
22
|
-
throw new HttpException(err.response.data, err.response.status);
|
|
23
|
-
} else if (err.request) {
|
|
24
|
-
throw new HttpException(err.request, 500);
|
|
25
|
-
} else {
|
|
26
|
-
throw new InternalServerErrorException(err.message);
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
async addActivityHistory(record, entity, method) {
|
|
31
|
-
const activity =
|
|
32
|
-
method === 'create'
|
|
33
|
-
? 'Insert new'
|
|
34
|
-
: method === 'update'
|
|
35
|
-
? 'Update'
|
|
36
|
-
: 'Delete';
|
|
37
|
-
|
|
38
|
-
const filteredRecord = omit(record.EntityValueAfter, [
|
|
39
|
-
'CreatedById',
|
|
40
|
-
'CreatedAt',
|
|
41
|
-
'UpdatedById',
|
|
42
|
-
'UpdatedAt',
|
|
43
|
-
]);
|
|
44
|
-
|
|
45
|
-
const filteredRecordBefore = pick(
|
|
46
|
-
record.EntityValueBefore,
|
|
47
|
-
keys(filteredRecord),
|
|
48
|
-
);
|
|
49
|
-
|
|
50
|
-
const payload = {
|
|
51
|
-
Action: record.Action || (method === 'create' ? 'Insert' : activity),
|
|
52
|
-
Activity: record.Activity || `${activity} ${entity}`,
|
|
53
|
-
Description: record.Description || `${activity} details of ${entity}`,
|
|
54
|
-
EntityType: record?.EntityType || entity,
|
|
55
|
-
EntityValueBefore:
|
|
56
|
-
method === 'create'
|
|
57
|
-
? {}
|
|
58
|
-
: method === 'update'
|
|
59
|
-
? filteredRecordBefore
|
|
60
|
-
: record.EntityValueBefore,
|
|
61
|
-
EntityValueAfter: record.EntityId
|
|
62
|
-
? omitBy(filteredRecord, record.EntityId)
|
|
63
|
-
: filteredRecord,
|
|
64
|
-
PerformedById: record.PerformedById,
|
|
65
|
-
PerformedAt: record.PerformedAt || Date.now(),
|
|
66
|
-
EntityId: record.EntityId ? record.EntityId : cuid(),
|
|
67
|
-
};
|
|
68
|
-
|
|
69
|
-
try {
|
|
70
|
-
const { data } = await axios.post(
|
|
71
|
-
`${this.commonApiUrl}/activity-histories`,
|
|
72
|
-
payload,
|
|
73
|
-
);
|
|
74
|
-
return data.EntityId;
|
|
75
|
-
} catch (err) {
|
|
76
|
-
this.handleAxiosError(err);
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
async getList(listName: string): Promise<[]> {
|
|
81
|
-
try {
|
|
82
|
-
const { data } = await axios.get(`${this.commonApiUrl}/lists/items`, {
|
|
83
|
-
params: { ListName: listName },
|
|
84
|
-
});
|
|
85
|
-
return data?.rows;
|
|
86
|
-
} catch (err) {
|
|
87
|
-
this.handleAxiosError(err);
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
async addFieldTranslation(payload: AddFieldTranslationDto) {
|
|
92
|
-
try {
|
|
93
|
-
await axios.post(`${this.commonApiUrl}/field-translations`, payload);
|
|
94
|
-
} catch (err) {
|
|
95
|
-
this.handleAxiosError(err);
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
async getMedia(query: GetMediaDto) {
|
|
100
|
-
try {
|
|
101
|
-
const { data } = await axios.get(`${this.commonApiUrl}/medias`, {
|
|
102
|
-
params: query,
|
|
103
|
-
});
|
|
104
|
-
return data?.rows;
|
|
105
|
-
} catch (err) {
|
|
106
|
-
this.handleAxiosError(err);
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
HttpException,
|
|
3
|
+
Injectable,
|
|
4
|
+
InternalServerErrorException,
|
|
5
|
+
} from '@nestjs/common';
|
|
6
|
+
import { ConfigService } from '@nestjs/config';
|
|
7
|
+
import axios from 'axios';
|
|
8
|
+
import * as cuid from 'cuid';
|
|
9
|
+
import { omit, omitBy, keys, pick } from 'lodash';
|
|
10
|
+
import { AddFieldTranslationDto } from './dto/add-field-translation.dto';
|
|
11
|
+
import { GetMediaDto } from './dto/get-media.dto';
|
|
12
|
+
@Injectable()
|
|
13
|
+
export class CommonService {
|
|
14
|
+
private readonly commonApiUrl: string;
|
|
15
|
+
|
|
16
|
+
constructor(private configService: ConfigService) {
|
|
17
|
+
this.commonApiUrl = this.configService.get('COMMON_API_URL');
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
handleAxiosError(err) {
|
|
21
|
+
if (err.response) {
|
|
22
|
+
throw new HttpException(err.response.data, err.response.status);
|
|
23
|
+
} else if (err.request) {
|
|
24
|
+
throw new HttpException(err.request, 500);
|
|
25
|
+
} else {
|
|
26
|
+
throw new InternalServerErrorException(err.message);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
async addActivityHistory(record, entity, method) {
|
|
31
|
+
const activity =
|
|
32
|
+
method === 'create'
|
|
33
|
+
? 'Insert new'
|
|
34
|
+
: method === 'update'
|
|
35
|
+
? 'Update'
|
|
36
|
+
: 'Delete';
|
|
37
|
+
|
|
38
|
+
const filteredRecord = omit(record.EntityValueAfter, [
|
|
39
|
+
'CreatedById',
|
|
40
|
+
'CreatedAt',
|
|
41
|
+
'UpdatedById',
|
|
42
|
+
'UpdatedAt',
|
|
43
|
+
]);
|
|
44
|
+
|
|
45
|
+
const filteredRecordBefore = pick(
|
|
46
|
+
record.EntityValueBefore,
|
|
47
|
+
keys(filteredRecord),
|
|
48
|
+
);
|
|
49
|
+
|
|
50
|
+
const payload = {
|
|
51
|
+
Action: record.Action || (method === 'create' ? 'Insert' : activity),
|
|
52
|
+
Activity: record.Activity || `${activity} ${entity}`,
|
|
53
|
+
Description: record.Description || `${activity} details of ${entity}`,
|
|
54
|
+
EntityType: record?.EntityType || entity,
|
|
55
|
+
EntityValueBefore:
|
|
56
|
+
method === 'create'
|
|
57
|
+
? {}
|
|
58
|
+
: method === 'update'
|
|
59
|
+
? filteredRecordBefore
|
|
60
|
+
: record.EntityValueBefore,
|
|
61
|
+
EntityValueAfter: record.EntityId
|
|
62
|
+
? omitBy(filteredRecord, record.EntityId)
|
|
63
|
+
: filteredRecord,
|
|
64
|
+
PerformedById: record.PerformedById,
|
|
65
|
+
PerformedAt: record.PerformedAt || Date.now(),
|
|
66
|
+
EntityId: record.EntityId ? record.EntityId : cuid(),
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
try {
|
|
70
|
+
const { data } = await axios.post(
|
|
71
|
+
`${this.commonApiUrl}/activity-histories`,
|
|
72
|
+
payload,
|
|
73
|
+
);
|
|
74
|
+
return data.EntityId;
|
|
75
|
+
} catch (err) {
|
|
76
|
+
this.handleAxiosError(err);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
async getList(listName: string): Promise<[]> {
|
|
81
|
+
try {
|
|
82
|
+
const { data } = await axios.get(`${this.commonApiUrl}/lists/items`, {
|
|
83
|
+
params: { ListName: listName },
|
|
84
|
+
});
|
|
85
|
+
return data?.rows;
|
|
86
|
+
} catch (err) {
|
|
87
|
+
this.handleAxiosError(err);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
async addFieldTranslation(payload: AddFieldTranslationDto) {
|
|
92
|
+
try {
|
|
93
|
+
await axios.post(`${this.commonApiUrl}/field-translations`, payload);
|
|
94
|
+
} catch (err) {
|
|
95
|
+
this.handleAxiosError(err);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
async getMedia(query: GetMediaDto) {
|
|
100
|
+
try {
|
|
101
|
+
const { data } = await axios.get(`${this.commonApiUrl}/medias`, {
|
|
102
|
+
params: query,
|
|
103
|
+
});
|
|
104
|
+
return data?.rows;
|
|
105
|
+
} catch (err) {
|
|
106
|
+
this.handleAxiosError(err);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|