328 lines
14 KiB
JavaScript
328 lines
14 KiB
JavaScript
"use strict";
|
|
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
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;
|
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
};
|
|
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
};
|
|
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
return function (target, key) { decorator(target, key, paramIndex); }
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.ProductsController = void 0;
|
|
const common_1 = require("@nestjs/common");
|
|
const throttler_1 = require("@nestjs/throttler");
|
|
const public_decorator_1 = require("../auth/decorators/public.decorator");
|
|
const create_product_dto_1 = require("./dto/create-product.dto");
|
|
const update_product_dto_1 = require("./dto/update-product.dto");
|
|
const products_service_1 = require("./products.service");
|
|
const merge_products_dto_1 = require("./dto/merge-products.dto");
|
|
const update_canonical_name_dto_1 = require("./dto/update-canonical-name.dto");
|
|
const set_tags_dto_1 = require("./dto/set-tags.dto");
|
|
const upsert_nutrition_dto_1 = require("./dto/upsert-nutrition.dto");
|
|
const bulk_update_products_dto_1 = require("./dto/bulk-update-products.dto");
|
|
const ai_categorize_bulk_dto_1 = require("./dto/ai-categorize-bulk.dto");
|
|
const set_product_status_dto_1 = require("./dto/set-product-status.dto");
|
|
const roles_decorator_1 = require("../auth/decorators/roles.decorator");
|
|
const ai_service_1 = require("../ai/ai.service");
|
|
const categories_service_1 = require("../categories/categories.service");
|
|
const premium_or_admin_guard_1 = require("../auth/premium-or-admin.guard");
|
|
let ProductsController = class ProductsController {
|
|
constructor(productsService, aiService, categoriesService) {
|
|
this.productsService = productsService;
|
|
this.aiService = aiService;
|
|
this.categoriesService = categoriesService;
|
|
}
|
|
findAll(tag) {
|
|
return this.productsService.findAll({ tag });
|
|
}
|
|
findAllTags() {
|
|
return this.productsService.findAllTags();
|
|
}
|
|
findDuplicates() {
|
|
return this.productsService.findDuplicateCandidates();
|
|
}
|
|
previewMerge(sourceProductId, targetProductId) {
|
|
return this.productsService.previewMerge(sourceProductId, targetProductId);
|
|
}
|
|
backfillCanonical() {
|
|
return this.productsService.backfillCanonicalNames();
|
|
}
|
|
findPending() {
|
|
return this.productsService.findPending();
|
|
}
|
|
aiCategorizeBulk(body) {
|
|
return this.productsService.aiCategorizeBulk(body.productIds);
|
|
}
|
|
findDeleted() {
|
|
return this.productsService.findDeleted();
|
|
}
|
|
findMine(req) {
|
|
return this.productsService.findByOwner(req.user.id);
|
|
}
|
|
findOne(id) {
|
|
return this.productsService.findOne(id);
|
|
}
|
|
createPrivate(body, req) {
|
|
return this.productsService.createPrivate(body, req.user.id);
|
|
}
|
|
async suggestCategory(id) {
|
|
const product = await this.productsService.findOne(id);
|
|
const categories = await this.categoriesService.findFlattened();
|
|
return this.aiService.suggestCategory(product.canonicalName ?? product.name, categories);
|
|
}
|
|
create(body, req) {
|
|
return this.productsService.create(body, req.user.id);
|
|
}
|
|
createPending(body, req) {
|
|
return this.productsService.createPending(body, req.user.id);
|
|
}
|
|
merge(body) {
|
|
return this.productsService.merge(body.sourceProductId, body.targetProductId);
|
|
}
|
|
updateCanonicalName(id, body) {
|
|
return this.productsService.updateCanonicalName(id, body.canonicalName);
|
|
}
|
|
setTags(id, body) {
|
|
return this.productsService.setTags(id, body.tags);
|
|
}
|
|
upsertNutrition(id, body) {
|
|
return this.productsService.upsertNutrition(id, body);
|
|
}
|
|
update(id, body) {
|
|
return this.productsService.update(id, body);
|
|
}
|
|
permanentDelete(id) {
|
|
return this.productsService.permanentDelete(id);
|
|
}
|
|
remove(id) {
|
|
return this.productsService.remove(id);
|
|
}
|
|
setStatus(id, body) {
|
|
return this.productsService.setStatus(id, body.status);
|
|
}
|
|
restore(id) {
|
|
return this.productsService.restore(id);
|
|
}
|
|
resetAll() {
|
|
return this.productsService.resetAll();
|
|
}
|
|
bulkUpdate(body) {
|
|
return this.productsService.bulkUpdate(body.ids, { categoryId: body.categoryId });
|
|
}
|
|
};
|
|
exports.ProductsController = ProductsController;
|
|
__decorate([
|
|
(0, public_decorator_1.Public)(),
|
|
(0, common_1.Get)(),
|
|
__param(0, (0, common_1.Query)('tag')),
|
|
__metadata("design:type", Function),
|
|
__metadata("design:paramtypes", [String]),
|
|
__metadata("design:returntype", void 0)
|
|
], ProductsController.prototype, "findAll", null);
|
|
__decorate([
|
|
(0, public_decorator_1.Public)(),
|
|
(0, common_1.Get)('tags'),
|
|
__metadata("design:type", Function),
|
|
__metadata("design:paramtypes", []),
|
|
__metadata("design:returntype", void 0)
|
|
], ProductsController.prototype, "findAllTags", null);
|
|
__decorate([
|
|
(0, roles_decorator_1.Roles)('admin'),
|
|
(0, common_1.Get)('duplicates'),
|
|
__metadata("design:type", Function),
|
|
__metadata("design:paramtypes", []),
|
|
__metadata("design:returntype", void 0)
|
|
], ProductsController.prototype, "findDuplicates", null);
|
|
__decorate([
|
|
(0, roles_decorator_1.Roles)('admin'),
|
|
(0, common_1.Get)('merge-preview'),
|
|
__param(0, (0, common_1.Query)('sourceProductId', common_1.ParseIntPipe)),
|
|
__param(1, (0, common_1.Query)('targetProductId', common_1.ParseIntPipe)),
|
|
__metadata("design:type", Function),
|
|
__metadata("design:paramtypes", [Number, Number]),
|
|
__metadata("design:returntype", void 0)
|
|
], ProductsController.prototype, "previewMerge", null);
|
|
__decorate([
|
|
(0, roles_decorator_1.Roles)('admin'),
|
|
(0, common_1.Post)('backfill-canonical'),
|
|
__metadata("design:type", Function),
|
|
__metadata("design:paramtypes", []),
|
|
__metadata("design:returntype", void 0)
|
|
], ProductsController.prototype, "backfillCanonical", null);
|
|
__decorate([
|
|
(0, roles_decorator_1.Roles)('admin'),
|
|
(0, common_1.Get)('pending'),
|
|
__metadata("design:type", Function),
|
|
__metadata("design:paramtypes", []),
|
|
__metadata("design:returntype", void 0)
|
|
], ProductsController.prototype, "findPending", null);
|
|
__decorate([
|
|
(0, roles_decorator_1.Roles)('admin'),
|
|
(0, common_1.Post)('ai-categorize-bulk'),
|
|
(0, throttler_1.Throttle)({ default: { ttl: 60_000, limit: 5 } }),
|
|
(0, common_1.HttpCode)(200),
|
|
__param(0, (0, common_1.Body)()),
|
|
__metadata("design:type", Function),
|
|
__metadata("design:paramtypes", [ai_categorize_bulk_dto_1.AiCategorizeBulkDto]),
|
|
__metadata("design:returntype", void 0)
|
|
], ProductsController.prototype, "aiCategorizeBulk", null);
|
|
__decorate([
|
|
(0, roles_decorator_1.Roles)('admin'),
|
|
(0, common_1.Get)('deleted'),
|
|
__metadata("design:type", Function),
|
|
__metadata("design:paramtypes", []),
|
|
__metadata("design:returntype", void 0)
|
|
], ProductsController.prototype, "findDeleted", null);
|
|
__decorate([
|
|
(0, common_1.Get)('mine'),
|
|
__param(0, (0, common_1.Request)()),
|
|
__metadata("design:type", Function),
|
|
__metadata("design:paramtypes", [Object]),
|
|
__metadata("design:returntype", void 0)
|
|
], ProductsController.prototype, "findMine", null);
|
|
__decorate([
|
|
(0, common_1.Get)(':id'),
|
|
__param(0, (0, common_1.Param)('id', common_1.ParseIntPipe)),
|
|
__metadata("design:type", Function),
|
|
__metadata("design:paramtypes", [Number]),
|
|
__metadata("design:returntype", void 0)
|
|
], ProductsController.prototype, "findOne", null);
|
|
__decorate([
|
|
(0, common_1.Post)('private'),
|
|
__param(0, (0, common_1.Body)()),
|
|
__param(1, (0, common_1.Request)()),
|
|
__metadata("design:type", Function),
|
|
__metadata("design:paramtypes", [create_product_dto_1.CreateProductDto, Object]),
|
|
__metadata("design:returntype", void 0)
|
|
], ProductsController.prototype, "createPrivate", null);
|
|
__decorate([
|
|
(0, common_1.UseGuards)(premium_or_admin_guard_1.PremiumOrAdminGuard),
|
|
(0, common_1.Get)(':id/suggest-category'),
|
|
(0, throttler_1.Throttle)({ default: { ttl: 60_000, limit: 20 } }),
|
|
__param(0, (0, common_1.Param)('id', common_1.ParseIntPipe)),
|
|
__metadata("design:type", Function),
|
|
__metadata("design:paramtypes", [Number]),
|
|
__metadata("design:returntype", Promise)
|
|
], ProductsController.prototype, "suggestCategory", null);
|
|
__decorate([
|
|
(0, roles_decorator_1.Roles)('admin'),
|
|
(0, common_1.Post)(),
|
|
__param(0, (0, common_1.Body)()),
|
|
__param(1, (0, common_1.Request)()),
|
|
__metadata("design:type", Function),
|
|
__metadata("design:paramtypes", [create_product_dto_1.CreateProductDto, Object]),
|
|
__metadata("design:returntype", void 0)
|
|
], ProductsController.prototype, "create", null);
|
|
__decorate([
|
|
(0, common_1.Post)('pending'),
|
|
__param(0, (0, common_1.Body)()),
|
|
__param(1, (0, common_1.Request)()),
|
|
__metadata("design:type", Function),
|
|
__metadata("design:paramtypes", [create_product_dto_1.CreateProductDto, Object]),
|
|
__metadata("design:returntype", void 0)
|
|
], ProductsController.prototype, "createPending", null);
|
|
__decorate([
|
|
(0, roles_decorator_1.Roles)('admin'),
|
|
(0, common_1.Post)('merge'),
|
|
__param(0, (0, common_1.Body)()),
|
|
__metadata("design:type", Function),
|
|
__metadata("design:paramtypes", [merge_products_dto_1.MergeProductsDto]),
|
|
__metadata("design:returntype", void 0)
|
|
], ProductsController.prototype, "merge", null);
|
|
__decorate([
|
|
(0, roles_decorator_1.Roles)('admin'),
|
|
(0, common_1.Patch)(':id/canonical-name'),
|
|
__param(0, (0, common_1.Param)('id', common_1.ParseIntPipe)),
|
|
__param(1, (0, common_1.Body)()),
|
|
__metadata("design:type", Function),
|
|
__metadata("design:paramtypes", [Number, update_canonical_name_dto_1.UpdateCanonicalNameDto]),
|
|
__metadata("design:returntype", void 0)
|
|
], ProductsController.prototype, "updateCanonicalName", null);
|
|
__decorate([
|
|
(0, roles_decorator_1.Roles)('admin'),
|
|
(0, common_1.Put)(':id/tags'),
|
|
__param(0, (0, common_1.Param)('id', common_1.ParseIntPipe)),
|
|
__param(1, (0, common_1.Body)()),
|
|
__metadata("design:type", Function),
|
|
__metadata("design:paramtypes", [Number, set_tags_dto_1.SetTagsDto]),
|
|
__metadata("design:returntype", void 0)
|
|
], ProductsController.prototype, "setTags", null);
|
|
__decorate([
|
|
(0, roles_decorator_1.Roles)('admin'),
|
|
(0, common_1.Put)(':id/nutrition'),
|
|
__param(0, (0, common_1.Param)('id', common_1.ParseIntPipe)),
|
|
__param(1, (0, common_1.Body)()),
|
|
__metadata("design:type", Function),
|
|
__metadata("design:paramtypes", [Number, upsert_nutrition_dto_1.UpsertNutritionDto]),
|
|
__metadata("design:returntype", void 0)
|
|
], ProductsController.prototype, "upsertNutrition", null);
|
|
__decorate([
|
|
(0, roles_decorator_1.Roles)('admin'),
|
|
(0, common_1.Patch)(':id'),
|
|
__param(0, (0, common_1.Param)('id', common_1.ParseIntPipe)),
|
|
__param(1, (0, common_1.Body)()),
|
|
__metadata("design:type", Function),
|
|
__metadata("design:paramtypes", [Number, update_product_dto_1.UpdateProductDto]),
|
|
__metadata("design:returntype", void 0)
|
|
], ProductsController.prototype, "update", null);
|
|
__decorate([
|
|
(0, roles_decorator_1.Roles)('admin'),
|
|
(0, common_1.Delete)(':id/permanent'),
|
|
__param(0, (0, common_1.Param)('id', common_1.ParseIntPipe)),
|
|
__metadata("design:type", Function),
|
|
__metadata("design:paramtypes", [Number]),
|
|
__metadata("design:returntype", void 0)
|
|
], ProductsController.prototype, "permanentDelete", null);
|
|
__decorate([
|
|
(0, roles_decorator_1.Roles)('admin'),
|
|
(0, common_1.Delete)(':id'),
|
|
__param(0, (0, common_1.Param)('id', common_1.ParseIntPipe)),
|
|
__metadata("design:type", Function),
|
|
__metadata("design:paramtypes", [Number]),
|
|
__metadata("design:returntype", void 0)
|
|
], ProductsController.prototype, "remove", null);
|
|
__decorate([
|
|
(0, roles_decorator_1.Roles)('admin'),
|
|
(0, common_1.Patch)(':id/status'),
|
|
__param(0, (0, common_1.Param)('id', common_1.ParseIntPipe)),
|
|
__param(1, (0, common_1.Body)()),
|
|
__metadata("design:type", Function),
|
|
__metadata("design:paramtypes", [Number, set_product_status_dto_1.SetProductStatusDto]),
|
|
__metadata("design:returntype", void 0)
|
|
], ProductsController.prototype, "setStatus", null);
|
|
__decorate([
|
|
(0, roles_decorator_1.Roles)('admin'),
|
|
(0, common_1.Post)(':id/restore'),
|
|
__param(0, (0, common_1.Param)('id', common_1.ParseIntPipe)),
|
|
__metadata("design:type", Function),
|
|
__metadata("design:paramtypes", [Number]),
|
|
__metadata("design:returntype", void 0)
|
|
], ProductsController.prototype, "restore", null);
|
|
__decorate([
|
|
(0, roles_decorator_1.Roles)('admin'),
|
|
(0, common_1.Post)('reset-all'),
|
|
(0, common_1.HttpCode)(200),
|
|
__metadata("design:type", Function),
|
|
__metadata("design:paramtypes", []),
|
|
__metadata("design:returntype", void 0)
|
|
], ProductsController.prototype, "resetAll", null);
|
|
__decorate([
|
|
(0, roles_decorator_1.Roles)('admin'),
|
|
(0, common_1.Post)('bulk-update'),
|
|
(0, common_1.HttpCode)(200),
|
|
__param(0, (0, common_1.Body)()),
|
|
__metadata("design:type", Function),
|
|
__metadata("design:paramtypes", [bulk_update_products_dto_1.BulkUpdateProductsDto]),
|
|
__metadata("design:returntype", void 0)
|
|
], ProductsController.prototype, "bulkUpdate", null);
|
|
exports.ProductsController = ProductsController = __decorate([
|
|
(0, common_1.Controller)('products'),
|
|
__metadata("design:paramtypes", [products_service_1.ProductsService,
|
|
ai_service_1.AiService,
|
|
categories_service_1.CategoriesService])
|
|
], ProductsController);
|
|
//# sourceMappingURL=products.controller.js.map
|