@hedhog/admin 0.0.108 → 0.0.109
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/admin.module.d.ts.map +1 -1
- package/dist/admin.module.js +1 -3
- package/dist/admin.module.js.map +1 -1
- package/dist/auth/auth.controller.d.ts.map +1 -1
- package/dist/auth/auth.controller.js +5 -6
- package/dist/auth/auth.controller.js.map +1 -1
- package/dist/auth/guards/auth.guard.js +2 -2
- package/dist/auth/guards/auth.guard.js.map +1 -1
- package/dist/index.d.ts +0 -7
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +0 -8
- package/dist/index.js.map +1 -1
- package/dist/menu/menu.controller.js +3 -3
- package/dist/menu/menu.controller.js.map +1 -1
- package/dist/role/guards/role.guard.d.ts.map +1 -1
- package/dist/role/guards/role.guard.js +3 -4
- package/dist/role/guards/role.guard.js.map +1 -1
- package/dist/role/role.controller.js +3 -3
- package/dist/role/role.controller.js.map +1 -1
- package/dist/role/role.module.js +1 -1
- package/dist/role/role.module.js.map +1 -1
- package/dist/role/role.service.d.ts +1 -1
- package/dist/role/role.service.d.ts.map +1 -1
- package/dist/role/role.service.js +1 -1
- package/dist/role/role.service.js.map +1 -1
- package/dist/route/route.controller.js +3 -3
- package/dist/route/route.controller.js.map +1 -1
- package/dist/screen/screen.controller.js +3 -3
- package/dist/screen/screen.controller.js.map +1 -1
- package/dist/setting/setting.controller.d.ts +1 -0
- package/dist/setting/setting.controller.d.ts.map +1 -1
- package/dist/setting/setting.controller.js +14 -3
- package/dist/setting/setting.controller.js.map +1 -1
- package/dist/setting/setting.module.d.ts.map +1 -1
- package/dist/setting/setting.module.js +2 -0
- package/dist/setting/setting.module.js.map +1 -1
- package/dist/setting/setting.service.d.ts +2 -0
- package/dist/setting/setting.service.d.ts.map +1 -1
- package/dist/setting/setting.service.js +63 -0
- package/dist/setting/setting.service.js.map +1 -1
- package/dist/user/user.controller.js +2 -2
- package/dist/user/user.controller.js.map +1 -1
- package/hedhog.yaml +1 -21
- package/package.json +8 -6
- package/src/admin.module.ts +1 -3
- package/src/auth/auth.controller.ts +1 -2
- package/src/auth/guards/auth.guard.ts +1 -1
- package/src/index.ts +0 -10
- package/src/menu/menu.controller.ts +2 -2
- package/src/role/guards/role.guard.ts +1 -2
- package/src/role/role.controller.ts +2 -2
- package/src/role/role.module.ts +1 -1
- package/src/role/role.service.ts +1 -1
- package/src/route/route.controller.ts +2 -2
- package/src/screen/screen.controller.ts +2 -2
- package/src/setting/setting.controller.ts +10 -3
- package/src/setting/setting.module.ts +2 -0
- package/src/setting/setting.service.ts +73 -0
- package/src/user/user.controller.ts +1 -1
- package/dist/auth/decorators/public.decorator.d.ts +0 -3
- package/dist/auth/decorators/public.decorator.d.ts.map +0 -1
- package/dist/auth/decorators/public.decorator.js +0 -8
- package/dist/auth/decorators/public.decorator.js.map +0 -1
- package/dist/locale/dto/create.dto.d.ts +0 -6
- package/dist/locale/dto/create.dto.d.ts.map +0 -1
- package/dist/locale/dto/create.dto.js +0 -29
- package/dist/locale/dto/create.dto.js.map +0 -1
- package/dist/locale/dto/delete.dto.d.ts +0 -4
- package/dist/locale/dto/delete.dto.d.ts.map +0 -1
- package/dist/locale/dto/delete.dto.js +0 -23
- package/dist/locale/dto/delete.dto.js.map +0 -1
- package/dist/locale/dto/set-enabled.dto.d.ts +0 -4
- package/dist/locale/dto/set-enabled.dto.d.ts.map +0 -1
- package/dist/locale/dto/set-enabled.dto.js +0 -24
- package/dist/locale/dto/set-enabled.dto.js.map +0 -1
- package/dist/locale/dto/update.dto.d.ts +0 -6
- package/dist/locale/dto/update.dto.d.ts.map +0 -1
- package/dist/locale/dto/update.dto.js +0 -32
- package/dist/locale/dto/update.dto.js.map +0 -1
- package/dist/locale/index.d.ts +0 -5
- package/dist/locale/index.d.ts.map +0 -1
- package/dist/locale/index.js +0 -21
- package/dist/locale/index.js.map +0 -1
- package/dist/locale/locale.controller.d.ts +0 -18
- package/dist/locale/locale.controller.d.ts.map +0 -1
- package/dist/locale/locale.controller.js +0 -127
- package/dist/locale/locale.controller.js.map +0 -1
- package/dist/locale/locale.decorator.d.ts +0 -2
- package/dist/locale/locale.decorator.d.ts.map +0 -1
- package/dist/locale/locale.decorator.js +0 -9
- package/dist/locale/locale.decorator.js.map +0 -1
- package/dist/locale/locale.middleware.d.ts +0 -10
- package/dist/locale/locale.middleware.d.ts.map +0 -1
- package/dist/locale/locale.middleware.js +0 -45
- package/dist/locale/locale.middleware.js.map +0 -1
- package/dist/locale/locale.module.d.ts +0 -5
- package/dist/locale/locale.module.d.ts.map +0 -1
- package/dist/locale/locale.module.js +0 -35
- package/dist/locale/locale.module.js.map +0 -1
- package/dist/locale/locale.service.d.ts +0 -32
- package/dist/locale/locale.service.d.ts.map +0 -1
- package/dist/locale/locale.service.js +0 -290
- package/dist/locale/locale.service.js.map +0 -1
- package/dist/locale/locale.service.spec.d.ts +0 -2
- package/dist/locale/locale.service.spec.d.ts.map +0 -1
- package/dist/locale/locale.service.spec.js +0 -174
- package/dist/locale/locale.service.spec.js.map +0 -1
- package/dist/role/decorators/role.decorator.d.ts +0 -3
- package/dist/role/decorators/role.decorator.d.ts.map +0 -1
- package/dist/role/decorators/role.decorator.js +0 -10
- package/dist/role/decorators/role.decorator.js.map +0 -1
- package/src/auth/decorators/public.decorator.ts +0 -4
- package/src/locale/dto/create.dto.ts +0 -12
- package/src/locale/dto/delete.dto.ts +0 -8
- package/src/locale/dto/set-enabled.dto.ts +0 -9
- package/src/locale/dto/update.dto.ts +0 -15
- package/src/locale/index.ts +0 -4
- package/src/locale/locale.controller.ts +0 -79
- package/src/locale/locale.decorator.ts +0 -8
- package/src/locale/locale.middleware.ts +0 -34
- package/src/locale/locale.module.ts +0 -23
- package/src/locale/locale.service.spec.ts +0 -193
- package/src/locale/locale.service.ts +0 -364
- package/src/role/decorators/role.decorator.ts +0 -7
@@ -1,34 +0,0 @@
|
|
1
|
-
import { PrismaService } from '@hedhog/prisma';
|
2
|
-
import { Injectable, NestMiddleware } from '@nestjs/common';
|
3
|
-
import { NextFunction, Request, Response } from 'express';
|
4
|
-
|
5
|
-
@Injectable()
|
6
|
-
export class LocaleMiddleware implements NestMiddleware {
|
7
|
-
private languages = [];
|
8
|
-
|
9
|
-
constructor(private prisma: PrismaService) {}
|
10
|
-
|
11
|
-
async use(req: Request, _res: Response, next: NextFunction) {
|
12
|
-
const locale = req.headers['accept-language'] || 'en-US';
|
13
|
-
let code = locale.split(',')[0].split('-')[0];
|
14
|
-
|
15
|
-
if (!this.languages.length) {
|
16
|
-
const locale = await this.prisma.locale.findMany({
|
17
|
-
select: {
|
18
|
-
code: true,
|
19
|
-
},
|
20
|
-
});
|
21
|
-
|
22
|
-
for (const l of locale) {
|
23
|
-
this.languages.push(l.code);
|
24
|
-
}
|
25
|
-
}
|
26
|
-
|
27
|
-
if (!this.languages.includes(code)) {
|
28
|
-
code = 'en';
|
29
|
-
}
|
30
|
-
|
31
|
-
req['locale'] = code;
|
32
|
-
next();
|
33
|
-
}
|
34
|
-
}
|
@@ -1,23 +0,0 @@
|
|
1
|
-
import { PaginationModule } from '@hedhog/pagination';
|
2
|
-
import { PrismaModule } from '@hedhog/prisma';
|
3
|
-
import { forwardRef, MiddlewareConsumer, Module } from '@nestjs/common';
|
4
|
-
import { LocaleService } from './locale.service';
|
5
|
-
import { LocaleController } from './locale.controller';
|
6
|
-
import { LocaleMiddleware } from './locale.middleware';
|
7
|
-
import { AuthModule } from '../auth/auth.module';
|
8
|
-
|
9
|
-
@Module({
|
10
|
-
imports: [
|
11
|
-
forwardRef(() => AuthModule),
|
12
|
-
forwardRef(() => PrismaModule),
|
13
|
-
forwardRef(() => PaginationModule),
|
14
|
-
],
|
15
|
-
controllers: [LocaleController],
|
16
|
-
providers: [LocaleService],
|
17
|
-
exports: [LocaleService],
|
18
|
-
})
|
19
|
-
export class LocaleModule {
|
20
|
-
configure(consumer: MiddlewareConsumer) {
|
21
|
-
consumer.apply(LocaleMiddleware).forRoutes('*');
|
22
|
-
}
|
23
|
-
}
|
@@ -1,193 +0,0 @@
|
|
1
|
-
import { PaginationService } from '@hedhog/pagination';
|
2
|
-
import { PrismaService } from '@hedhog/prisma';
|
3
|
-
import { BadRequestException } from '@nestjs/common';
|
4
|
-
import { Test, TestingModule } from '@nestjs/testing';
|
5
|
-
import { CreateDTO } from './dto/create.dto';
|
6
|
-
import { DeleteDTO } from './dto/delete.dto';
|
7
|
-
import { UpdateDTO } from './dto/update.dto';
|
8
|
-
import { LocaleService } from './locale.service';
|
9
|
-
|
10
|
-
describe('LocaleService', () => {
|
11
|
-
let service: LocaleService;
|
12
|
-
let prismaService: PrismaService;
|
13
|
-
let paginationService: PaginationService;
|
14
|
-
|
15
|
-
beforeEach(async () => {
|
16
|
-
const module: TestingModule = await Test.createTestingModule({
|
17
|
-
providers: [
|
18
|
-
LocaleService,
|
19
|
-
{
|
20
|
-
provide: PrismaService,
|
21
|
-
useValue: {
|
22
|
-
translation: {
|
23
|
-
findMany: jest.fn(),
|
24
|
-
},
|
25
|
-
locale: {
|
26
|
-
findUnique: jest.fn(),
|
27
|
-
create: jest.fn(),
|
28
|
-
update: jest.fn(),
|
29
|
-
deleteMany: jest.fn(),
|
30
|
-
},
|
31
|
-
},
|
32
|
-
},
|
33
|
-
{
|
34
|
-
provide: PaginationService,
|
35
|
-
useValue: {
|
36
|
-
paginate: jest.fn(),
|
37
|
-
},
|
38
|
-
},
|
39
|
-
],
|
40
|
-
}).compile();
|
41
|
-
|
42
|
-
service = module.get<LocaleService>(LocaleService);
|
43
|
-
prismaService = module.get<PrismaService>(PrismaService);
|
44
|
-
paginationService = module.get<PaginationService>(PaginationService);
|
45
|
-
});
|
46
|
-
|
47
|
-
it('should be defined', () => {
|
48
|
-
expect(service).toBeDefined();
|
49
|
-
});
|
50
|
-
|
51
|
-
describe('getTranslations', () => {
|
52
|
-
it('should throw BadRequestException when localeCode is not provided', async () => {
|
53
|
-
await expect(service.getTranslations('', 'namespace')).rejects.toThrow(
|
54
|
-
BadRequestException,
|
55
|
-
);
|
56
|
-
});
|
57
|
-
|
58
|
-
it('should return translation for a given localeCode and namespace', async () => {
|
59
|
-
const mockTranslations = [
|
60
|
-
{
|
61
|
-
id: 1,
|
62
|
-
locale_id: 1,
|
63
|
-
namespace_id: 1,
|
64
|
-
name: 'hello',
|
65
|
-
value: 'Hello',
|
66
|
-
created_at: new Date(),
|
67
|
-
updated_at: new Date(),
|
68
|
-
},
|
69
|
-
];
|
70
|
-
jest
|
71
|
-
.spyOn(prismaService.translation, 'findMany')
|
72
|
-
.mockResolvedValue(mockTranslations);
|
73
|
-
|
74
|
-
const result = await service.getTranslations('en-US', 'namespace');
|
75
|
-
expect(result).toEqual({ hello: 'Hello' });
|
76
|
-
});
|
77
|
-
});
|
78
|
-
/*
|
79
|
-
describe('get', () => {
|
80
|
-
it('should paginate locale', async () => {
|
81
|
-
const mockPaginationResult = {
|
82
|
-
data: [],
|
83
|
-
total: 10,
|
84
|
-
lastPage: 1,
|
85
|
-
page: 1,
|
86
|
-
prev: 0,
|
87
|
-
next: 2,
|
88
|
-
pageSize: 10,
|
89
|
-
};
|
90
|
-
jest
|
91
|
-
.spyOn(paginationService, 'paginate')
|
92
|
-
.mockResolvedValue(mockPaginationResult);
|
93
|
-
|
94
|
-
const paginationParams: PaginationDTO = {
|
95
|
-
page: 1,
|
96
|
-
pageSize: 20,
|
97
|
-
sortField: '',
|
98
|
-
sortOrder: PageOrderDirection.Asc,
|
99
|
-
fields: '',
|
100
|
-
search: '',
|
101
|
-
};
|
102
|
-
const result = await service.get(paginationParams );
|
103
|
-
|
104
|
-
expect(paginationService.paginate).toHaveBeenCalledWith(
|
105
|
-
prismaService.locale,
|
106
|
-
paginationParams,
|
107
|
-
expect.anything(),
|
108
|
-
);
|
109
|
-
expect(result).toBe(mockPaginationResult);
|
110
|
-
});
|
111
|
-
});
|
112
|
-
*/
|
113
|
-
describe('getById', () => {
|
114
|
-
it('should return a locale by ID', async () => {
|
115
|
-
const mockLocale = {
|
116
|
-
id: 1,
|
117
|
-
name: 'English',
|
118
|
-
code: 'en',
|
119
|
-
region: 'us',
|
120
|
-
created_at: new Date(),
|
121
|
-
updated_at: new Date(),
|
122
|
-
};
|
123
|
-
jest
|
124
|
-
.spyOn(prismaService.locale, 'findUnique')
|
125
|
-
.mockResolvedValue(mockLocale);
|
126
|
-
|
127
|
-
const result = await service.getById(1);
|
128
|
-
expect(result).toBe(mockLocale);
|
129
|
-
});
|
130
|
-
});
|
131
|
-
|
132
|
-
describe('create', () => {
|
133
|
-
it('should create a new locale', async () => {
|
134
|
-
const mockCreateDTO: CreateDTO = {
|
135
|
-
name: 'English',
|
136
|
-
code: 'en',
|
137
|
-
region: 'us',
|
138
|
-
};
|
139
|
-
const mockLocale = {
|
140
|
-
id: 1,
|
141
|
-
name: 'English',
|
142
|
-
code: 'en',
|
143
|
-
region: 'us',
|
144
|
-
created_at: new Date(),
|
145
|
-
updated_at: new Date(),
|
146
|
-
};
|
147
|
-
jest.spyOn(prismaService.locale, 'create').mockResolvedValue(mockLocale);
|
148
|
-
|
149
|
-
const result = await service.create(mockCreateDTO);
|
150
|
-
expect(result).toBe(mockLocale);
|
151
|
-
});
|
152
|
-
});
|
153
|
-
|
154
|
-
describe('update', () => {
|
155
|
-
it('should update a locale', async () => {
|
156
|
-
const mockUpdateDTO: UpdateDTO = {
|
157
|
-
name: 'English Updated',
|
158
|
-
code: 'en',
|
159
|
-
region: 'us',
|
160
|
-
};
|
161
|
-
const mockLocale = {
|
162
|
-
id: 1,
|
163
|
-
name: 'English Updated',
|
164
|
-
code: 'en',
|
165
|
-
region: 'us',
|
166
|
-
created_at: new Date(),
|
167
|
-
updated_at: new Date(),
|
168
|
-
};
|
169
|
-
jest.spyOn(prismaService.locale, 'update').mockResolvedValue(mockLocale);
|
170
|
-
|
171
|
-
const result = await service.update({ id: 1, data: mockUpdateDTO });
|
172
|
-
expect(result).toBe(mockLocale);
|
173
|
-
});
|
174
|
-
});
|
175
|
-
|
176
|
-
describe('delete', () => {
|
177
|
-
it('should throw BadRequestException if no IDs are provided', async () => {
|
178
|
-
await expect(service.delete({ ids: null })).rejects.toThrow(
|
179
|
-
BadRequestException,
|
180
|
-
);
|
181
|
-
});
|
182
|
-
|
183
|
-
it('should delete locale by IDs', async () => {
|
184
|
-
const mockDeleteDTO: DeleteDTO = { ids: [1, 2, 3] };
|
185
|
-
jest
|
186
|
-
.spyOn(prismaService.locale, 'deleteMany')
|
187
|
-
.mockResolvedValue({ count: 3 });
|
188
|
-
|
189
|
-
const result = await service.delete(mockDeleteDTO);
|
190
|
-
expect(result.count).toBe(3);
|
191
|
-
});
|
192
|
-
});
|
193
|
-
});
|
@@ -1,364 +0,0 @@
|
|
1
|
-
import { PaginationDTO, PaginationService } from '@hedhog/pagination';
|
2
|
-
import { PrismaService } from '@hedhog/prisma';
|
3
|
-
import {
|
4
|
-
BadRequestException,
|
5
|
-
Inject,
|
6
|
-
Injectable,
|
7
|
-
forwardRef,
|
8
|
-
} from '@nestjs/common';
|
9
|
-
import { CreateDTO } from './dto/create.dto';
|
10
|
-
import { DeleteDTO } from './dto/delete.dto';
|
11
|
-
import { UpdateDTO } from './dto/update.dto';
|
12
|
-
|
13
|
-
@Injectable()
|
14
|
-
export class LocaleService {
|
15
|
-
private codes: any = {};
|
16
|
-
|
17
|
-
constructor(
|
18
|
-
@Inject(forwardRef(() => PrismaService))
|
19
|
-
private readonly prismaService: PrismaService,
|
20
|
-
@Inject(forwardRef(() => PaginationService))
|
21
|
-
private readonly paginationService: PaginationService,
|
22
|
-
) {}
|
23
|
-
|
24
|
-
async setEnabled(codes: string[]) {
|
25
|
-
if (!codes || codes.length < 1) {
|
26
|
-
throw new BadRequestException('You must select at least one item code.');
|
27
|
-
}
|
28
|
-
|
29
|
-
await this.prismaService.locale.updateMany({
|
30
|
-
where: {
|
31
|
-
enabled: true,
|
32
|
-
},
|
33
|
-
data: {
|
34
|
-
enabled: false,
|
35
|
-
},
|
36
|
-
});
|
37
|
-
|
38
|
-
return this.prismaService.locale.updateMany({
|
39
|
-
where: {
|
40
|
-
code: {
|
41
|
-
in: codes,
|
42
|
-
},
|
43
|
-
},
|
44
|
-
data: {
|
45
|
-
enabled: true,
|
46
|
-
},
|
47
|
-
});
|
48
|
-
}
|
49
|
-
|
50
|
-
async getEnables(currentLocale: string, paginationParams: PaginationDTO) {
|
51
|
-
const fields = ['code', 'region'];
|
52
|
-
const OR: any[] = this.prismaService.createInsensitiveSearch(
|
53
|
-
fields,
|
54
|
-
paginationParams,
|
55
|
-
);
|
56
|
-
|
57
|
-
const result = await this.paginationService.paginate(
|
58
|
-
this.prismaService.locale,
|
59
|
-
paginationParams,
|
60
|
-
{
|
61
|
-
where: {
|
62
|
-
AND: [
|
63
|
-
{
|
64
|
-
enabled: true,
|
65
|
-
},
|
66
|
-
{
|
67
|
-
OR,
|
68
|
-
},
|
69
|
-
],
|
70
|
-
},
|
71
|
-
},
|
72
|
-
);
|
73
|
-
|
74
|
-
const codes = [];
|
75
|
-
|
76
|
-
for (const item of result.data) {
|
77
|
-
codes.push((item as any).code);
|
78
|
-
}
|
79
|
-
|
80
|
-
const { code, region, locale } = this.parseLocale(currentLocale);
|
81
|
-
|
82
|
-
const where: any = {
|
83
|
-
locale: {
|
84
|
-
code,
|
85
|
-
},
|
86
|
-
translation_namespace: {
|
87
|
-
name: 'translation',
|
88
|
-
},
|
89
|
-
};
|
90
|
-
|
91
|
-
if (locale.length > 1) {
|
92
|
-
where.locale.region = region;
|
93
|
-
}
|
94
|
-
|
95
|
-
const values = await this.prismaService.translation.findMany({
|
96
|
-
where,
|
97
|
-
select: {
|
98
|
-
name: true,
|
99
|
-
value: true,
|
100
|
-
},
|
101
|
-
});
|
102
|
-
|
103
|
-
for (let i = 0; i < result.data.length; i++) {
|
104
|
-
for (const value of values) {
|
105
|
-
if (value.name === (result.data[i] as any).code) {
|
106
|
-
(result.data[i] as any).name = value.value;
|
107
|
-
break;
|
108
|
-
}
|
109
|
-
}
|
110
|
-
}
|
111
|
-
|
112
|
-
return result;
|
113
|
-
}
|
114
|
-
|
115
|
-
parseLocale(locale: string) {
|
116
|
-
const localeCodes = locale.toLowerCase().split('-');
|
117
|
-
const code = localeCodes[0];
|
118
|
-
const region = localeCodes[1];
|
119
|
-
return {
|
120
|
-
code,
|
121
|
-
region,
|
122
|
-
locale: localeCodes,
|
123
|
-
};
|
124
|
-
}
|
125
|
-
|
126
|
-
async getTranslations(localeCode: string, namespace: string) {
|
127
|
-
if (!localeCode) {
|
128
|
-
throw new BadRequestException('Locale code is required.');
|
129
|
-
}
|
130
|
-
|
131
|
-
if (!namespace) {
|
132
|
-
namespace = 'translation';
|
133
|
-
}
|
134
|
-
|
135
|
-
const { code, region, locale } = this.parseLocale(localeCode);
|
136
|
-
const where: any = {
|
137
|
-
locale: {
|
138
|
-
code,
|
139
|
-
},
|
140
|
-
translation_namespace: {
|
141
|
-
name: namespace,
|
142
|
-
},
|
143
|
-
};
|
144
|
-
|
145
|
-
if (locale.length > 1) {
|
146
|
-
where.locale.region = region;
|
147
|
-
}
|
148
|
-
|
149
|
-
const values = await this.prismaService.translation.findMany({
|
150
|
-
where,
|
151
|
-
select: {
|
152
|
-
name: true,
|
153
|
-
value: true,
|
154
|
-
},
|
155
|
-
});
|
156
|
-
|
157
|
-
const translation = {};
|
158
|
-
|
159
|
-
for (const value of values) {
|
160
|
-
translation[value.name] = value.value;
|
161
|
-
}
|
162
|
-
|
163
|
-
return translation;
|
164
|
-
}
|
165
|
-
|
166
|
-
async get(currentLocale: string, paginationParams: PaginationDTO) {
|
167
|
-
const fields = ['code', 'region'];
|
168
|
-
const OR: any[] = this.prismaService.createInsensitiveSearch(
|
169
|
-
fields,
|
170
|
-
paginationParams,
|
171
|
-
);
|
172
|
-
|
173
|
-
const result = await this.paginationService.paginate(
|
174
|
-
this.prismaService.locale,
|
175
|
-
paginationParams,
|
176
|
-
{
|
177
|
-
where: {
|
178
|
-
OR,
|
179
|
-
},
|
180
|
-
},
|
181
|
-
);
|
182
|
-
|
183
|
-
const codes = [];
|
184
|
-
|
185
|
-
for (const item of result.data) {
|
186
|
-
codes.push((item as any).code);
|
187
|
-
}
|
188
|
-
|
189
|
-
const { code, region, locale } = this.parseLocale(currentLocale);
|
190
|
-
|
191
|
-
const where: any = {
|
192
|
-
locale: {
|
193
|
-
code,
|
194
|
-
},
|
195
|
-
translation_namespace: {
|
196
|
-
name: 'translation',
|
197
|
-
},
|
198
|
-
};
|
199
|
-
|
200
|
-
if (locale.length > 1) {
|
201
|
-
where.locale.region = region;
|
202
|
-
}
|
203
|
-
|
204
|
-
const values = await this.prismaService.translation.findMany({
|
205
|
-
where,
|
206
|
-
select: {
|
207
|
-
name: true,
|
208
|
-
value: true,
|
209
|
-
},
|
210
|
-
});
|
211
|
-
|
212
|
-
for (let i = 0; i < result.data.length; i++) {
|
213
|
-
for (const value of values) {
|
214
|
-
if (value.name === (result.data[i] as any).code) {
|
215
|
-
(result.data[i] as any).name = value.value;
|
216
|
-
break;
|
217
|
-
}
|
218
|
-
}
|
219
|
-
}
|
220
|
-
|
221
|
-
return result;
|
222
|
-
}
|
223
|
-
|
224
|
-
async getByCode(code: string) {
|
225
|
-
if (this.codes[code]) {
|
226
|
-
return this.codes[code];
|
227
|
-
}
|
228
|
-
return (this.codes[code] = await this.prismaService.locale.findFirst({
|
229
|
-
where: { code },
|
230
|
-
}));
|
231
|
-
}
|
232
|
-
|
233
|
-
async getById(localeId: number) {
|
234
|
-
return this.prismaService.locale.findUnique({
|
235
|
-
where: { id: localeId },
|
236
|
-
});
|
237
|
-
}
|
238
|
-
|
239
|
-
async create(data: CreateDTO) {
|
240
|
-
return this.prismaService.locale.create({
|
241
|
-
data,
|
242
|
-
});
|
243
|
-
}
|
244
|
-
|
245
|
-
async update({ id, data }: { id: number; data: UpdateDTO }) {
|
246
|
-
return this.prismaService.locale.update({
|
247
|
-
where: { id },
|
248
|
-
data,
|
249
|
-
});
|
250
|
-
}
|
251
|
-
|
252
|
-
async delete({ ids }: DeleteDTO) {
|
253
|
-
if (ids == undefined || ids == null) {
|
254
|
-
throw new BadRequestException(
|
255
|
-
'You must select at least one item to delete.',
|
256
|
-
);
|
257
|
-
}
|
258
|
-
|
259
|
-
return this.prismaService.locale.deleteMany({
|
260
|
-
where: {
|
261
|
-
id: {
|
262
|
-
in: ids,
|
263
|
-
},
|
264
|
-
},
|
265
|
-
});
|
266
|
-
}
|
267
|
-
|
268
|
-
private getTableNameTranslations(modelName: string) {
|
269
|
-
return `${modelName}_locale`;
|
270
|
-
}
|
271
|
-
|
272
|
-
async createModelWithLocale<T>(
|
273
|
-
modelName: string,
|
274
|
-
foreignKeyName: string,
|
275
|
-
data: T,
|
276
|
-
locale: Record<string, Record<string, string>>,
|
277
|
-
) {
|
278
|
-
try {
|
279
|
-
const model = await this.prismaService[modelName].create({ data });
|
280
|
-
|
281
|
-
if (locale) {
|
282
|
-
await Promise.all(
|
283
|
-
Object.entries(locale).map(async ([localeCode, localeData]) => {
|
284
|
-
const localeRecord = await this.getByCode(localeCode);
|
285
|
-
const localeEntry = {
|
286
|
-
[foreignKeyName]: model.id,
|
287
|
-
locale_id: localeRecord.id,
|
288
|
-
...localeData,
|
289
|
-
};
|
290
|
-
|
291
|
-
await this.prismaService[
|
292
|
-
this.getTableNameTranslations(modelName)
|
293
|
-
].create({
|
294
|
-
data: localeEntry,
|
295
|
-
});
|
296
|
-
}),
|
297
|
-
);
|
298
|
-
}
|
299
|
-
|
300
|
-
return this.prismaService[modelName].findUnique({
|
301
|
-
where: { id: model.id },
|
302
|
-
include: {
|
303
|
-
[this.getTableNameTranslations(modelName)]: {
|
304
|
-
where: { locale: { enabled: true } },
|
305
|
-
include: { locale: { select: { code: true } } },
|
306
|
-
},
|
307
|
-
},
|
308
|
-
});
|
309
|
-
} catch (error: any) {
|
310
|
-
if (error.message.includes('Unique constraint failed')) {
|
311
|
-
throw new BadRequestException('Data already exists.');
|
312
|
-
} else {
|
313
|
-
throw new BadRequestException(error);
|
314
|
-
}
|
315
|
-
}
|
316
|
-
}
|
317
|
-
|
318
|
-
async updateModelWithLocale<T>(
|
319
|
-
modelName: string,
|
320
|
-
foreignKeyName: string,
|
321
|
-
id: number,
|
322
|
-
data: T,
|
323
|
-
locale: Record<string, Record<string, string>>,
|
324
|
-
) {
|
325
|
-
try {
|
326
|
-
if (locale) {
|
327
|
-
await Promise.all(
|
328
|
-
Object.entries(locale).map(async ([localeCode, localeData]) => {
|
329
|
-
const localeRecord = await this.getByCode(localeCode);
|
330
|
-
const localeEntry = {
|
331
|
-
[foreignKeyName]: id,
|
332
|
-
locale_id: localeRecord.id,
|
333
|
-
...localeData,
|
334
|
-
};
|
335
|
-
|
336
|
-
await this.prismaService[
|
337
|
-
this.getTableNameTranslations(modelName)
|
338
|
-
].upsert({
|
339
|
-
where: {
|
340
|
-
[`${foreignKeyName}_locale_id`]: {
|
341
|
-
[foreignKeyName]: id,
|
342
|
-
locale_id: localeRecord.id,
|
343
|
-
},
|
344
|
-
},
|
345
|
-
create: localeEntry,
|
346
|
-
update: localeData,
|
347
|
-
});
|
348
|
-
}),
|
349
|
-
);
|
350
|
-
}
|
351
|
-
|
352
|
-
return this.prismaService[modelName].update({
|
353
|
-
where: { id },
|
354
|
-
data,
|
355
|
-
});
|
356
|
-
} catch (error: any) {
|
357
|
-
if (error.message.includes('Unique constraint failed')) {
|
358
|
-
throw new BadRequestException('Data already exists.');
|
359
|
-
} else {
|
360
|
-
throw new BadRequestException(error);
|
361
|
-
}
|
362
|
-
}
|
363
|
-
}
|
364
|
-
}
|