@hed-hog/category 0.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.
@@ -0,0 +1,27 @@
1
+ import { CategoryService } from './category.service';
2
+ import { CategoryDTO } from './dto/category.dto';
3
+ export declare class CategoryController {
4
+ private readonly categoryService;
5
+ constructor(categoryService: CategoryService);
6
+ getStats(): Promise<{
7
+ total: any;
8
+ totalActive: any;
9
+ totalInactive: any;
10
+ totalRoot: any;
11
+ }>;
12
+ getById(id: number): Promise<any>;
13
+ getByParent(categoryId: string): Promise<any>;
14
+ findAll(paginationParams: any, locale: string, status: string, parent: string): Promise<{
15
+ total: any;
16
+ lastPage: number;
17
+ page: number;
18
+ pageSize: number;
19
+ prev: number;
20
+ next: number;
21
+ data: any;
22
+ }>;
23
+ delete(id: number): Promise<any>;
24
+ create(locale: string, { name, slug, category_id, color, icon, status }: CategoryDTO): Promise<any>;
25
+ update(id: number, locale: string, { name, slug, category_id, color, icon, status }: Partial<CategoryDTO>): Promise<any>;
26
+ }
27
+ //# sourceMappingURL=category.controller.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"category.controller.d.ts","sourceRoot":"","sources":["../src/category.controller.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AACrD,OAAO,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AAEjD,qBACa,kBAAkB;IAE3B,OAAO,CAAC,QAAQ,CAAC,eAAe;gBAAf,eAAe,EAAE,eAAe;IAI7C,QAAQ;;;;;;IAKR,OAAO,CAA4B,EAAE,EAAE,MAAM;IAK7C,WAAW,CAAsB,UAAU,EAAE,MAAM;IAKnD,OAAO,CACG,gBAAgB,KAAA,EACpB,MAAM,EAAE,MAAM,EACP,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,MAAM;;;;;;;;;IAM3B,MAAM,CAA4B,EAAE,EAAE,MAAM;IAK5C,MAAM,CACA,MAAM,EAAE,MAAM,EAChB,EAAE,IAAI,EAAE,IAAI,EAAE,WAAW,EAAE,KAAK,EAAE,IAAI,EAAE,MAAM,EAAE,EAAE,WAAW;IAgBjE,MAAM,CACiB,EAAE,EAAE,MAAM,EAC3B,MAAM,EAAE,MAAM,EAChB,EAAE,IAAI,EAAE,IAAI,EAAE,WAAW,EAAE,KAAK,EAAE,IAAI,EAAE,MAAM,EAAE,EAAE,OAAO,CAAC,WAAW,CAAC;CAejF"}
@@ -0,0 +1,120 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ var __param = (this && this.__param) || function (paramIndex, decorator) {
12
+ return function (target, key) { decorator(target, key, paramIndex); }
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.CategoryController = void 0;
16
+ const api_locale_1 = require("@hed-hog/api-locale");
17
+ const api_pagination_1 = require("@hed-hog/api-pagination");
18
+ const common_1 = require("@nestjs/common");
19
+ const category_service_1 = require("./category.service");
20
+ const category_dto_1 = require("./dto/category.dto");
21
+ let CategoryController = class CategoryController {
22
+ constructor(categoryService) {
23
+ this.categoryService = categoryService;
24
+ }
25
+ async getStats() {
26
+ return this.categoryService.getStats();
27
+ }
28
+ async getById(id) {
29
+ return this.categoryService.getById(id);
30
+ }
31
+ async getByParent(categoryId) {
32
+ return this.categoryService.getByParent(categoryId);
33
+ }
34
+ async findAll(paginationParams, locale, status, parent) {
35
+ return this.categoryService.findAll(paginationParams, locale, status, parent);
36
+ }
37
+ async delete(id) {
38
+ return this.categoryService.delete(id);
39
+ }
40
+ async create(locale, { name, slug, category_id, color, icon, status }) {
41
+ return this.categoryService.create(locale, {
42
+ name,
43
+ slug,
44
+ category_id,
45
+ color,
46
+ icon,
47
+ status,
48
+ });
49
+ }
50
+ async update(id, locale, { name, slug, category_id, color, icon, status }) {
51
+ return this.categoryService.update(id, locale, {
52
+ name,
53
+ slug,
54
+ category_id,
55
+ color,
56
+ icon,
57
+ status,
58
+ });
59
+ }
60
+ };
61
+ exports.CategoryController = CategoryController;
62
+ __decorate([
63
+ (0, common_1.Get)('stats'),
64
+ __metadata("design:type", Function),
65
+ __metadata("design:paramtypes", []),
66
+ __metadata("design:returntype", Promise)
67
+ ], CategoryController.prototype, "getStats", null);
68
+ __decorate([
69
+ (0, common_1.Get)(':id'),
70
+ __param(0, (0, common_1.Param)('id', common_1.ParseIntPipe)),
71
+ __metadata("design:type", Function),
72
+ __metadata("design:paramtypes", [Number]),
73
+ __metadata("design:returntype", Promise)
74
+ ], CategoryController.prototype, "getById", null);
75
+ __decorate([
76
+ (0, common_1.Get)('parent/:categoryId'),
77
+ __param(0, (0, common_1.Param)('categoryId')),
78
+ __metadata("design:type", Function),
79
+ __metadata("design:paramtypes", [String]),
80
+ __metadata("design:returntype", Promise)
81
+ ], CategoryController.prototype, "getByParent", null);
82
+ __decorate([
83
+ (0, common_1.Get)(),
84
+ __param(0, (0, api_pagination_1.Pagination)()),
85
+ __param(1, (0, api_locale_1.Locale)()),
86
+ __param(2, (0, common_1.Query)('status')),
87
+ __param(3, (0, common_1.Query)('parent')),
88
+ __metadata("design:type", Function),
89
+ __metadata("design:paramtypes", [Object, String, String, String]),
90
+ __metadata("design:returntype", Promise)
91
+ ], CategoryController.prototype, "findAll", null);
92
+ __decorate([
93
+ (0, common_1.Delete)(':id'),
94
+ __param(0, (0, common_1.Param)('id', common_1.ParseIntPipe)),
95
+ __metadata("design:type", Function),
96
+ __metadata("design:paramtypes", [Number]),
97
+ __metadata("design:returntype", Promise)
98
+ ], CategoryController.prototype, "delete", null);
99
+ __decorate([
100
+ (0, common_1.Post)(),
101
+ __param(0, (0, api_locale_1.Locale)()),
102
+ __param(1, (0, common_1.Body)()),
103
+ __metadata("design:type", Function),
104
+ __metadata("design:paramtypes", [String, category_dto_1.CategoryDTO]),
105
+ __metadata("design:returntype", Promise)
106
+ ], CategoryController.prototype, "create", null);
107
+ __decorate([
108
+ (0, common_1.Patch)(':id'),
109
+ __param(0, (0, common_1.Param)('id', common_1.ParseIntPipe)),
110
+ __param(1, (0, api_locale_1.Locale)()),
111
+ __param(2, (0, common_1.Body)()),
112
+ __metadata("design:type", Function),
113
+ __metadata("design:paramtypes", [Number, String, Object]),
114
+ __metadata("design:returntype", Promise)
115
+ ], CategoryController.prototype, "update", null);
116
+ exports.CategoryController = CategoryController = __decorate([
117
+ (0, common_1.Controller)('category'),
118
+ __metadata("design:paramtypes", [category_service_1.CategoryService])
119
+ ], CategoryController);
120
+ //# sourceMappingURL=category.controller.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"category.controller.js","sourceRoot":"","sources":["../src/category.controller.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,oDAA6C;AAC7C,4DAAqD;AACrD,2CAUwB;AACxB,yDAAqD;AACrD,qDAAiD;AAG1C,IAAM,kBAAkB,GAAxB,MAAM,kBAAkB;IAC7B,YACmB,eAAgC;QAAhC,oBAAe,GAAf,eAAe,CAAiB;IAChD,CAAC;IAGE,AAAN,KAAK,CAAC,QAAQ;QACZ,OAAO,IAAI,CAAC,eAAe,CAAC,QAAQ,EAAE,CAAA;IACxC,CAAC;IAGK,AAAN,KAAK,CAAC,OAAO,CAA4B,EAAU;QACjD,OAAO,IAAI,CAAC,eAAe,CAAC,OAAO,CAAC,EAAE,CAAC,CAAA;IACzC,CAAC;IAGK,AAAN,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,EAAC,oBAAoB,CAAC;IACP,WAAA,IAAA,cAAK,EAAC,YAAY,CAAC,CAAA;;;;qDAErC;AAGK;IADL,IAAA,YAAG,GAAE;IAEH,WAAA,IAAA,2BAAU,GAAE,CAAA;IACZ,WAAA,IAAA,mBAAM,GAAE,CAAA;IACR,WAAA,IAAA,cAAK,EAAC,QAAQ,CAAC,CAAA;IACf,WAAA,IAAA,cAAK,EAAC,QAAQ,CAAC,CAAA;;;;iDAGjB;AAGK;IADL,IAAA,eAAM,EAAC,KAAK,CAAC;IACA,WAAA,IAAA,cAAK,EAAC,IAAI,EAAE,qBAAY,CAAC,CAAA;;;;gDAEtC;AAGK;IADL,IAAA,aAAI,GAAE;IAEJ,WAAA,IAAA,mBAAM,GAAE,CAAA;IACR,WAAA,IAAA,aAAI,GAAE,CAAA;;6CAAmD,0BAAW;;gDAatE;AAGK;IADL,IAAA,cAAK,EAAC,KAAK,CAAC;IAEV,WAAA,IAAA,cAAK,EAAC,IAAI,EAAE,qBAAY,CAAC,CAAA;IACzB,WAAA,IAAA,mBAAM,GAAE,CAAA;IACR,WAAA,IAAA,aAAI,GAAE,CAAA;;;;gDAcR;6BAvEU,kBAAkB;IAD9B,IAAA,mBAAU,EAAC,UAAU,CAAC;qCAGe,kCAAe;GAFxC,kBAAkB,CAwE9B"}
@@ -0,0 +1,3 @@
1
+ export declare class CategoryModule {
2
+ }
3
+ //# sourceMappingURL=category.module.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"category.module.d.ts","sourceRoot":"","sources":["../src/category.module.ts"],"names":[],"mappings":"AAKA,qBAMa,cAAc;CAAG"}
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.CategoryModule = void 0;
10
+ const api_locale_1 = require("@hed-hog/api-locale");
11
+ const common_1 = require("@nestjs/common");
12
+ const category_controller_1 = require("./category.controller");
13
+ const category_service_1 = require("./category.service");
14
+ let CategoryModule = class CategoryModule {
15
+ };
16
+ exports.CategoryModule = CategoryModule;
17
+ exports.CategoryModule = CategoryModule = __decorate([
18
+ (0, common_1.Module)({
19
+ imports: [(0, common_1.forwardRef)(() => api_locale_1.LocaleModule)],
20
+ controllers: [category_controller_1.CategoryController],
21
+ providers: [category_service_1.CategoryService],
22
+ exports: [category_service_1.CategoryService],
23
+ })
24
+ ], CategoryModule);
25
+ //# sourceMappingURL=category.module.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"category.module.js","sourceRoot":"","sources":["../src/category.module.ts"],"names":[],"mappings":";;;;;;;;;AAAA,oDAAmD;AACnD,2CAAoD;AACpD,+DAA2D;AAC3D,yDAAqD;AAQ9C,IAAM,cAAc,GAApB,MAAM,cAAc;CAAG,CAAA;AAAjB,wCAAc;yBAAd,cAAc;IAN1B,IAAA,eAAM,EAAC;QACN,OAAO,EAAE,CAAC,IAAA,mBAAU,EAAC,GAAG,EAAE,CAAC,yBAAY,CAAC,CAAC;QACzC,WAAW,EAAE,CAAC,wCAAkB,CAAC;QACjC,SAAS,EAAE,CAAC,kCAAe,CAAC;QAC5B,OAAO,EAAE,CAAC,kCAAe,CAAC;KAC3B,CAAC;GACW,cAAc,CAAG"}
@@ -0,0 +1,31 @@
1
+ import { LocaleService } from '@hed-hog/api-locale';
2
+ import { PaginationDTO, PaginationService } from '@hed-hog/api-pagination';
3
+ import { PrismaService } from '@hed-hog/api-prisma';
4
+ import { CategoryDTO } from './dto/category.dto';
5
+ export declare class CategoryService {
6
+ private readonly prisma;
7
+ private readonly pagination;
8
+ private readonly localeService;
9
+ constructor(prisma: PrismaService, pagination: PaginationService, localeService: LocaleService);
10
+ getStats(): Promise<{
11
+ total: any;
12
+ totalActive: any;
13
+ totalInactive: any;
14
+ totalRoot: any;
15
+ }>;
16
+ getById(id: number): Promise<any>;
17
+ getByParent(id: string): Promise<any>;
18
+ findAll(paginationParams: PaginationDTO, locale: string, status: string, parent: string): Promise<{
19
+ total: any;
20
+ lastPage: number;
21
+ page: number;
22
+ pageSize: number;
23
+ prev: number;
24
+ next: number;
25
+ data: any;
26
+ }>;
27
+ delete(id: number): Promise<any>;
28
+ create(locale: string, { name, slug, category_id, color, icon, status }: CategoryDTO): Promise<any>;
29
+ update(id: number, locale: string, { name, slug, category_id, color, icon, status }: Partial<CategoryDTO>): Promise<any>;
30
+ }
31
+ //# sourceMappingURL=category.service.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"category.service.d.ts","sourceRoot":"","sources":["../src/category.service.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACpD,OAAO,EAAE,aAAa,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAC;AAC3E,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAEpD,OAAO,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AAEjD,qBACa,eAAe;IAExB,OAAO,CAAC,QAAQ,CAAC,MAAM;IACvB,OAAO,CAAC,QAAQ,CAAC,UAAU;IAE3B,OAAO,CAAC,QAAQ,CAAC,aAAa;gBAHb,MAAM,EAAE,aAAa,EACrB,UAAU,EAAE,iBAAiB,EAE7B,aAAa,EAAE,aAAa;IAGzC,QAAQ;;;;;;IAoBR,OAAO,CAAC,EAAE,EAAE,MAAM;IAMlB,WAAW,CAAC,EAAE,EAAE,MAAM;IAatB,OAAO,CAAC,gBAAgB,EAAE,aAAa,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM;;;;;;;;;IAuCvF,MAAM,CAAC,EAAE,EAAE,MAAM;IAcjB,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE,WAAW,EAAE,KAAK,EAAE,IAAI,EAAE,MAAM,EAAE,EAAE,WAAW;IA2BpF,MAAM,CAAC,EAAE,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE,WAAW,EAAE,KAAK,EAAE,IAAI,EAAE,MAAM,EAAE,EAAE,OAAO,CAAC,WAAW,CAAC;CAmChH"}
@@ -0,0 +1,157 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ var __param = (this && this.__param) || function (paramIndex, decorator) {
12
+ return function (target, key) { decorator(target, key, paramIndex); }
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.CategoryService = void 0;
16
+ const api_1 = require("@hed-hog/api");
17
+ const api_locale_1 = require("@hed-hog/api-locale");
18
+ const api_pagination_1 = require("@hed-hog/api-pagination");
19
+ const api_prisma_1 = require("@hed-hog/api-prisma");
20
+ const common_1 = require("@nestjs/common");
21
+ let CategoryService = class CategoryService {
22
+ constructor(prisma, pagination, localeService) {
23
+ this.prisma = prisma;
24
+ this.pagination = pagination;
25
+ this.localeService = localeService;
26
+ }
27
+ async getStats() {
28
+ const total = await this.prisma.category.count({});
29
+ const totalActive = await this.prisma.category.count({
30
+ where: { status: 'active' },
31
+ });
32
+ const totalInactive = await this.prisma.category.count({
33
+ where: { status: 'inactive' },
34
+ });
35
+ const totalRoot = await this.prisma.category.count({
36
+ where: { category_id: null },
37
+ });
38
+ return {
39
+ total,
40
+ totalActive,
41
+ totalInactive,
42
+ totalRoot,
43
+ };
44
+ }
45
+ async getById(id) {
46
+ return this.prisma.category.findUnique({
47
+ where: { id: Number(id) },
48
+ });
49
+ }
50
+ async getByParent(id) {
51
+ const categories = await this.prisma.category.findMany({
52
+ where: { category_id: Number(id) },
53
+ include: {
54
+ category_locale: true,
55
+ },
56
+ });
57
+ return categories.map((category) => (0, api_1.itemTranslations)('category_locale', category));
58
+ }
59
+ async findAll(paginationParams, locale, status, parent) {
60
+ const fields = ['slug'];
61
+ const OR = this.prisma.createInsensitiveSearch(fields, paginationParams);
62
+ const AND = [];
63
+ if (status && status !== 'all') {
64
+ AND.push({ status });
65
+ }
66
+ if (parent && parent !== 'all') {
67
+ AND.push(parent === 'root'
68
+ ? { category_id: null }
69
+ : { category: { slug: parent } });
70
+ }
71
+ const result = await this.pagination.paginate(this.prisma.category, paginationParams, {
72
+ where: {
73
+ OR,
74
+ AND,
75
+ },
76
+ include: {
77
+ category_locale: true,
78
+ },
79
+ }, locale);
80
+ result.data = result.data.map((category) => (0, api_1.itemTranslations)('category_locale', category));
81
+ return result;
82
+ }
83
+ async delete(id) {
84
+ const category = await this.prisma.category.findUnique({
85
+ where: { id: Number(id) },
86
+ });
87
+ if (!category) {
88
+ throw new common_1.NotFoundException('Category not found');
89
+ }
90
+ return this.prisma.category.delete({
91
+ where: { id: Number(id) },
92
+ });
93
+ }
94
+ async create(locale, { name, slug, category_id, color, icon, status }) {
95
+ const localeRecord = await this.localeService.getByCode(locale);
96
+ if (!localeRecord) {
97
+ throw new common_1.BadRequestException(`Locale ${locale} not found`);
98
+ }
99
+ const category = await this.prisma.category.create({
100
+ data: {
101
+ slug,
102
+ category_id,
103
+ color,
104
+ icon,
105
+ status,
106
+ },
107
+ });
108
+ if (!category) {
109
+ throw new common_1.BadRequestException('Category not created');
110
+ }
111
+ await this.prisma.category_locale.create({
112
+ data: { name, category_id: category.id, locale_id: localeRecord.id },
113
+ });
114
+ return category;
115
+ }
116
+ async update(id, locale, { name, slug, category_id, color, icon, status }) {
117
+ const localeRecord = await this.localeService.getByCode(locale);
118
+ if (!localeRecord) {
119
+ throw new common_1.BadRequestException(`Locale ${locale} not found`);
120
+ }
121
+ const category = await this.prisma.category.findUnique({
122
+ where: { id: Number(id) },
123
+ });
124
+ if (!category) {
125
+ throw new common_1.NotFoundException('Category not found');
126
+ }
127
+ return this.prisma.category.update({
128
+ where: { id: Number(id) },
129
+ data: {
130
+ slug,
131
+ category_id,
132
+ color,
133
+ icon,
134
+ status,
135
+ category_locale: {
136
+ update: {
137
+ where: {
138
+ category_id_locale_id: { locale_id: localeRecord.id, category_id: Number(id) },
139
+ },
140
+ data: {
141
+ name,
142
+ },
143
+ },
144
+ },
145
+ },
146
+ });
147
+ }
148
+ };
149
+ exports.CategoryService = CategoryService;
150
+ exports.CategoryService = CategoryService = __decorate([
151
+ (0, common_1.Injectable)(),
152
+ __param(2, (0, common_1.Inject)((0, common_1.forwardRef)(() => api_locale_1.LocaleService))),
153
+ __metadata("design:paramtypes", [api_prisma_1.PrismaService,
154
+ api_pagination_1.PaginationService,
155
+ api_locale_1.LocaleService])
156
+ ], CategoryService);
157
+ //# sourceMappingURL=category.service.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"category.service.js","sourceRoot":"","sources":["../src/category.service.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,sCAAgD;AAChD,oDAAoD;AACpD,4DAA2E;AAC3E,oDAAoD;AACpD,2CAAwG;AAIjG,IAAM,eAAe,GAArB,MAAM,eAAe;IAC1B,YACmB,MA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AAa;QACT,kCAAiB;QAEd,0BAAa;GALpC,eAAe,CAkK3B"}
@@ -0,0 +1,9 @@
1
+ export declare class CategoryDTO {
2
+ slug: string;
3
+ category_id?: number;
4
+ color?: string;
5
+ icon?: string;
6
+ status?: 'active' | 'inactive';
7
+ name: string;
8
+ }
9
+ //# sourceMappingURL=category.dto.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"category.dto.d.ts","sourceRoot":"","sources":["../../src/dto/category.dto.ts"],"names":[],"mappings":"AASA,qBAAa,WAAW;IAGtB,IAAI,EAAE,MAAM,CAAC;IAIb,WAAW,CAAC,EAAE,MAAM,CAAC;IAOrB,KAAK,CAAC,EAAE,MAAM,CAAC;IAIf,IAAI,CAAC,EAAE,MAAM,CAAC;IAMd,MAAM,CAAC,EAAE,QAAQ,GAAG,UAAU,CAAC;IAI/B,IAAI,EAAE,MAAM,CAAC;CACd"}
@@ -0,0 +1,52 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.CategoryDTO = void 0;
13
+ const class_validator_1 = require("class-validator");
14
+ class CategoryDTO {
15
+ }
16
+ exports.CategoryDTO = CategoryDTO;
17
+ __decorate([
18
+ (0, class_validator_1.IsString)({ message: 'O slug deve ser uma string.' }),
19
+ (0, class_validator_1.IsNotEmpty)({ message: 'O slug é obrigatório.' }),
20
+ __metadata("design:type", String)
21
+ ], CategoryDTO.prototype, "slug", void 0);
22
+ __decorate([
23
+ (0, class_validator_1.IsOptional)(),
24
+ (0, class_validator_1.IsNumber)({}, { message: 'O ID da categoria deve ser um número.' }),
25
+ __metadata("design:type", Number)
26
+ ], CategoryDTO.prototype, "category_id", void 0);
27
+ __decorate([
28
+ (0, class_validator_1.IsOptional)(),
29
+ (0, class_validator_1.IsString)({ message: 'A cor deve ser uma string.' }),
30
+ (0, class_validator_1.Matches)(/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/, {
31
+ message: 'A cor deve ser um valor hexadecimal válido.',
32
+ }),
33
+ __metadata("design:type", String)
34
+ ], CategoryDTO.prototype, "color", void 0);
35
+ __decorate([
36
+ (0, class_validator_1.IsOptional)(),
37
+ (0, class_validator_1.IsString)({ message: 'O ícone deve ser uma string.' }),
38
+ __metadata("design:type", String)
39
+ ], CategoryDTO.prototype, "icon", void 0);
40
+ __decorate([
41
+ (0, class_validator_1.IsOptional)(),
42
+ (0, class_validator_1.IsEnum)(['active', 'inactive'], {
43
+ message: 'O status deve ser "active" ou "inactive".',
44
+ }),
45
+ __metadata("design:type", String)
46
+ ], CategoryDTO.prototype, "status", void 0);
47
+ __decorate([
48
+ (0, class_validator_1.IsString)({ message: 'O nome deve ser uma string.' }),
49
+ (0, class_validator_1.IsNotEmpty)({ message: 'O nome é obrigatório.' }),
50
+ __metadata("design:type", String)
51
+ ], CategoryDTO.prototype, "name", void 0);
52
+ //# sourceMappingURL=category.dto.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"category.dto.js","sourceRoot":"","sources":["../../src/dto/category.dto.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,qDAOyB;AAEzB,MAAa,WAAW;CA6BvB;AA7BD,kCA6BC;AA1BC;IAFC,IAAA,0BAAQ,EAAC,EAAE,OAAO,EAAE,6BAA6B,EAAE,CAAC;IACpD,IAAA,4BAAU,EAAC,EAAE,OAAO,EAAE,uBAAuB,EAAE,CAAC;;yCACpC;AAIb;IAFC,IAAA,4BAAU,GAAE;IACZ,IAAA,0BAAQ,EAAC,EAAE,EAAE,EAAE,OAAO,EAAE,uCAAuC,EAAE,CAAC;;gDAC9C;AAOrB;IALC,IAAA,4BAAU,GAAE;IACZ,IAAA,0BAAQ,EAAC,EAAE,OAAO,EAAE,4BAA4B,EAAE,CAAC;IACnD,IAAA,yBAAO,EAAC,oCAAoC,EAAE;QAC7C,OAAO,EAAE,6CAA6C;KACvD,CAAC;;0CACa;AAIf;IAFC,IAAA,4BAAU,GAAE;IACZ,IAAA,0BAAQ,EAAC,EAAE,OAAO,EAAE,8BAA8B,EAAE,CAAC;;yCACxC;AAMd;IAJC,IAAA,4BAAU,GAAE;IACZ,IAAA,wBAAM,EAAC,CAAC,QAAQ,EAAE,UAAU,CAAC,EAAE;QAC9B,OAAO,EAAE,2CAA2C;KACrD,CAAC;;2CAC6B;AAI/B;IAFC,IAAA,0BAAQ,EAAC,EAAE,OAAO,EAAE,6BAA6B,EAAE,CAAC;IACpD,IAAA,4BAAU,EAAC,EAAE,OAAO,EAAE,uBAAuB,EAAE,CAAC;;yCACpC"}
@@ -0,0 +1,2 @@
1
+ export * from './category.module';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,mBAAmB,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./category.module"), exports);
18
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,oDAAkC"}
@@ -0,0 +1,10 @@
1
+ - url: /category
2
+ icon: tag
3
+ name:
4
+ en: Categories
5
+ pt: Categorias
6
+ slug: /category
7
+ relations:
8
+ role:
9
+ - where:
10
+ slug: admin
@@ -0,0 +1,42 @@
1
+ - url: /category
2
+ method: GET
3
+ relations:
4
+ role:
5
+ - where:
6
+ slug: admin
7
+ - url: /category
8
+ method: POST
9
+ relations:
10
+ role:
11
+ - where:
12
+ slug: admin
13
+ - url: /category/stats
14
+ method: GET
15
+ relations:
16
+ role:
17
+ - where:
18
+ slug: admin
19
+ - url: /category/:id
20
+ method: GET
21
+ relations:
22
+ role:
23
+ - where:
24
+ slug: admin
25
+ - url: /category/parent/:categoryId
26
+ method: GET
27
+ relations:
28
+ role:
29
+ - where:
30
+ slug: admin
31
+ - url: /category/:id
32
+ method: PATCH
33
+ relations:
34
+ role:
35
+ - where:
36
+ slug: admin
37
+ - url: /category/:id
38
+ method: DELETE
39
+ relations:
40
+ role:
41
+ - where:
42
+ slug: admin
@@ -0,0 +1,22 @@
1
+ columns:
2
+ - type: pk
3
+ - type: slug
4
+ - name: icon
5
+ - name: color
6
+ - name: category_id
7
+ type: fk
8
+ references:
9
+ table: category
10
+ column: id
11
+ onDelete: CASCADE
12
+ - name: status
13
+ type: enum
14
+ values: [active, inactive]
15
+ - name: name
16
+ type: locale_varchar
17
+ length: 255
18
+ locale:
19
+ en: Name
20
+ pt: Nome
21
+ - type: created_at
22
+ - type: updated_at
package/package.json ADDED
@@ -0,0 +1,37 @@
1
+ {
2
+ "name": "@hed-hog/category",
3
+ "version": "0.0.0",
4
+ "main": "dist/index.js",
5
+ "types": "dist/index.d.ts",
6
+ "dependencies": {
7
+ "@nestjs/common": "*",
8
+ "@nestjs/config": "^4.0.2",
9
+ "@nestjs/core": "^11",
10
+ "@nestjs/jwt": "^11",
11
+ "@nestjs/mapped-types": "*",
12
+ "@hed-hog/api-locale": "0.0.10",
13
+ "@hed-hog/api": "0.0.3",
14
+ "@hed-hog/api-pagination": "0.0.3",
15
+ "@hed-hog/admin": "0.0.130",
16
+ "@hed-hog/api-prisma": "0.0.4"
17
+ },
18
+ "exports": {
19
+ ".": {
20
+ "import": "./dist/index.js",
21
+ "require": "./dist/index.js"
22
+ },
23
+ "./package.json": "./package.json"
24
+ },
25
+ "files": [
26
+ "dist",
27
+ "src",
28
+ "hedhog"
29
+ ],
30
+ "scripts": {
31
+ "lint": "eslint \"{src,apps,libs,test}/**/*.ts\"",
32
+ "prebuild": "npx ts-node ../../scripts/build-dependencies.ts libraries/tag",
33
+ "build": "tsc --project tsconfig.production.json",
34
+ "patch": "npx ts-node ../../scripts/patch.ts libraries/tag",
35
+ "prod": "pnpm run patch && pnpm run build && pnpm publish --access public --no-git-checks"
36
+ }
37
+ }
@@ -0,0 +1,90 @@
1
+ import { Locale } from '@hed-hog/api-locale';
2
+ import { Pagination } from '@hed-hog/api-pagination';
3
+ import {
4
+ Body,
5
+ Controller,
6
+ Delete,
7
+ Get,
8
+ Param,
9
+ ParseIntPipe,
10
+ Patch,
11
+ Post,
12
+ Query
13
+ } from '@nestjs/common';
14
+ import { CategoryService } from './category.service';
15
+ import { CategoryDTO } from './dto/category.dto';
16
+
17
+ @Controller('category')
18
+ export class CategoryController {
19
+ constructor(
20
+ private readonly categoryService: CategoryService
21
+ ) {}
22
+
23
+ @Get('stats')
24
+ async getStats() {
25
+ return this.categoryService.getStats()
26
+ }
27
+
28
+ @Get(':id')
29
+ async getById(@Param('id', ParseIntPipe) id: number) {
30
+ return this.categoryService.getById(id)
31
+ }
32
+
33
+ @Get('parent/:categoryId')
34
+ async getByParent(@Param('categoryId') categoryId: string) {
35
+ return this.categoryService.getByParent(categoryId)
36
+ }
37
+
38
+ @Get()
39
+ async findAll(
40
+ @Pagination() paginationParams,
41
+ @Locale() locale: string,
42
+ @Query('status') status: string,
43
+ @Query('parent') parent: string,
44
+ ) {
45
+ return this.categoryService.findAll(paginationParams, locale, status, parent)
46
+ }
47
+
48
+ @Delete(':id')
49
+ async delete(@Param('id', ParseIntPipe) id: number) {
50
+ return this.categoryService.delete(id);
51
+ }
52
+
53
+ @Post()
54
+ async create(
55
+ @Locale() locale: string,
56
+ @Body() { name, slug, category_id, color, icon, status }: CategoryDTO,
57
+ ) {
58
+ return this.categoryService.create(
59
+ locale,
60
+ {
61
+ name,
62
+ slug,
63
+ category_id,
64
+ color,
65
+ icon,
66
+ status,
67
+ }
68
+ );
69
+ }
70
+
71
+ @Patch(':id')
72
+ async update(
73
+ @Param('id', ParseIntPipe) id: number,
74
+ @Locale() locale: string,
75
+ @Body() { name, slug, category_id, color, icon, status }: Partial<CategoryDTO>,
76
+ ) {
77
+ return this.categoryService.update(
78
+ id,
79
+ locale,
80
+ {
81
+ name,
82
+ slug,
83
+ category_id,
84
+ color,
85
+ icon,
86
+ status,
87
+ }
88
+ );
89
+ }
90
+ }
@@ -0,0 +1,13 @@
1
+ import { LocaleModule } from '@hed-hog/api-locale';
2
+ import { Module, forwardRef } from '@nestjs/common';
3
+ import { CategoryController } from './category.controller';
4
+ import { CategoryService } from './category.service';
5
+
6
+ @Module({
7
+ imports: [forwardRef(() => LocaleModule)],
8
+ controllers: [CategoryController],
9
+ providers: [CategoryService],
10
+ exports: [CategoryService],
11
+ })
12
+ export class CategoryModule {}
13
+
@@ -0,0 +1,171 @@
1
+ import { itemTranslations } from '@hed-hog/api';
2
+ import { LocaleService } from '@hed-hog/api-locale';
3
+ import { PaginationDTO, PaginationService } from '@hed-hog/api-pagination';
4
+ import { PrismaService } from '@hed-hog/api-prisma';
5
+ import { BadRequestException, Inject, Injectable, NotFoundException, forwardRef } from '@nestjs/common';
6
+ import { CategoryDTO } from './dto/category.dto';
7
+
8
+ @Injectable()
9
+ export class CategoryService {
10
+ constructor(
11
+ private readonly prisma: PrismaService,
12
+ private readonly pagination: PaginationService,
13
+ @Inject(forwardRef(() => LocaleService))
14
+ private readonly localeService: LocaleService,
15
+ ) {}
16
+
17
+ async getStats(){
18
+ const total = await this.prisma.category.count({});
19
+ const totalActive = await this.prisma.category.count({
20
+ where: { status: 'active' },
21
+ });
22
+ const totalInactive = await this.prisma.category.count({
23
+ where: { status: 'inactive' },
24
+ });
25
+ const totalRoot = await this.prisma.category.count({
26
+ where: { category_id: null },
27
+ });
28
+
29
+ return {
30
+ total,
31
+ totalActive,
32
+ totalInactive,
33
+ totalRoot,
34
+ };
35
+ }
36
+
37
+ async getById(id: number){
38
+ return this.prisma.category.findUnique({
39
+ where: { id: Number(id) },
40
+ });
41
+ }
42
+
43
+ async getByParent(id: string){
44
+ const categories = await this.prisma.category.findMany({
45
+ where: { category_id: Number(id) },
46
+ include: {
47
+ category_locale: true,
48
+ },
49
+ });
50
+
51
+ return categories.map((category) =>
52
+ itemTranslations('category_locale', category),
53
+ );
54
+ }
55
+
56
+ async findAll(paginationParams: PaginationDTO, locale: string, status: string, parent: string){
57
+ const fields = ['slug'];
58
+ const OR = this.prisma.createInsensitiveSearch(fields, paginationParams);
59
+ const AND = [];
60
+
61
+ if (status && status !== 'all') {
62
+ AND.push({ status });
63
+ }
64
+
65
+ if (parent && parent !== 'all') {
66
+ AND.push(
67
+ parent === 'root'
68
+ ? { category_id: null }
69
+ : { category: { slug: parent } },
70
+ );
71
+ }
72
+
73
+ const result = await this.pagination.paginate(
74
+ this.prisma.category,
75
+ paginationParams,
76
+ {
77
+ where: {
78
+ OR,
79
+ AND,
80
+ },
81
+ include: {
82
+ category_locale: true,
83
+ },
84
+ },
85
+ locale,
86
+ );
87
+
88
+ result.data = result.data.map((category) =>
89
+ itemTranslations('category_locale', category),
90
+ );
91
+
92
+ return result;
93
+ }
94
+
95
+ async delete(id: number){
96
+ const category = await this.prisma.category.findUnique({
97
+ where: { id: Number(id) },
98
+ });
99
+
100
+ if (!category) {
101
+ throw new NotFoundException('Category not found');
102
+ }
103
+
104
+ return this.prisma.category.delete({
105
+ where: { id: Number(id) },
106
+ });
107
+ }
108
+
109
+ async create(locale: string, { name, slug, category_id, color, icon, status }: CategoryDTO){
110
+ const localeRecord = await this.localeService.getByCode(locale);
111
+ if (!localeRecord) {
112
+ throw new BadRequestException(`Locale ${locale} not found`);
113
+ }
114
+
115
+ const category = await this.prisma.category.create({
116
+ data: {
117
+ slug,
118
+ category_id,
119
+ color,
120
+ icon,
121
+ status,
122
+ },
123
+ });
124
+
125
+ if (!category) {
126
+ throw new BadRequestException('Category not created');
127
+ }
128
+
129
+ await this.prisma.category_locale.create({
130
+ data: { name, category_id: category.id, locale_id: localeRecord.id },
131
+ });
132
+
133
+ return category;
134
+ }
135
+
136
+ async update(id: number, locale: string, { name, slug, category_id, color, icon, status }: Partial<CategoryDTO>){
137
+ const localeRecord = await this.localeService.getByCode(locale);
138
+ if (!localeRecord) {
139
+ throw new BadRequestException(`Locale ${locale} not found`);
140
+ }
141
+
142
+ const category = await this.prisma.category.findUnique({
143
+ where: { id: Number(id) },
144
+ });
145
+
146
+ if (!category) {
147
+ throw new NotFoundException('Category not found');
148
+ }
149
+
150
+ return this.prisma.category.update({
151
+ where: { id: Number(id) },
152
+ data: {
153
+ slug,
154
+ category_id,
155
+ color,
156
+ icon,
157
+ status,
158
+ category_locale: {
159
+ update: {
160
+ where: {
161
+ category_id_locale_id: { locale_id: localeRecord.id, category_id: Number(id) },
162
+ },
163
+ data: {
164
+ name,
165
+ },
166
+ },
167
+ },
168
+ },
169
+ });
170
+ }
171
+ }
@@ -0,0 +1,39 @@
1
+ import {
2
+ IsEnum,
3
+ IsNotEmpty,
4
+ IsNumber,
5
+ IsOptional,
6
+ IsString,
7
+ Matches,
8
+ } from 'class-validator';
9
+
10
+ export class CategoryDTO {
11
+ @IsString({ message: 'O slug deve ser uma string.' })
12
+ @IsNotEmpty({ message: 'O slug é obrigatório.' })
13
+ slug: string;
14
+
15
+ @IsOptional()
16
+ @IsNumber({}, { message: 'O ID da categoria deve ser um número.' })
17
+ category_id?: number;
18
+
19
+ @IsOptional()
20
+ @IsString({ message: 'A cor deve ser uma string.' })
21
+ @Matches(/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/, {
22
+ message: 'A cor deve ser um valor hexadecimal válido.',
23
+ })
24
+ color?: string;
25
+
26
+ @IsOptional()
27
+ @IsString({ message: 'O ícone deve ser uma string.' })
28
+ icon?: string;
29
+
30
+ @IsOptional()
31
+ @IsEnum(['active', 'inactive'], {
32
+ message: 'O status deve ser "active" ou "inactive".',
33
+ })
34
+ status?: 'active' | 'inactive';
35
+
36
+ @IsString({ message: 'O nome deve ser uma string.' })
37
+ @IsNotEmpty({ message: 'O nome é obrigatório.' })
38
+ name: string;
39
+ }
package/src/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from './category.module';
2
+