UNPKG

opencart-manager

Version:

Node.js package helping to manage your opencart store data easily.

70 lines (69 loc) 3.23 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const attribute_1 = __importDefault(require("../entities/attribute")); const errors_1 = __importDefault(require("../errors")); const utils_1 = require("../utils"); const attribute_group_1 = __importDefault(require("./attribute-group")); class AttributeFactory { constructor(model) { this.model = model; this.group = new attribute_group_1.default(this.model); } create(attributeGroup) { if (!utils_1.isInteger(attributeGroup === null || attributeGroup === void 0 ? void 0 : attributeGroup.id)) throw new Error(errors_1.default.NOT_SPECIFIED("attributeGroup.id")); return new attribute_1.default(this.model, attributeGroup.id); } extract(criteria) { return __awaiter(this, void 0, void 0, function* () { const rowsData = yield this.model.attribute.select(criteria); return this.createEntityFromRowData(rowsData[0]); }); } extractAll(criteria) { return __awaiter(this, void 0, void 0, function* () { const rowsData = yield this.model.attribute.select(criteria); const entities = []; for (const rowData of rowsData) { const entity = yield this.createEntityFromRowData(rowData); if (entity) { entities.push(entity); } } return entities; }); } createEntityFromRowData(rowData) { return __awaiter(this, void 0, void 0, function* () { if (!rowData) return undefined; const { attributeId } = rowData; const { attributeGroupId } = rowData; const entity = new attribute_1.default(this.model, attributeGroupId, attributeId); entity.setData({ sortOrder: rowData.sortOrder }); const descriptionRowData = yield this.model.attribute.description.select({ attributeId, }); descriptionRowData.forEach((description) => { entity.setDescription({ languageId: description.languageId, name: description.name, }); }); return entity; }); } } exports.default = AttributeFactory;