opencart-manager
Version:
Node.js package helping to manage your opencart store data easily.
105 lines (104 loc) • 5 kB
JavaScript
"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 errors_1 = __importDefault(require("../errors"));
const utils_1 = require("../utils");
class Product {
constructor(model, _id) {
this.model = model;
this._id = _id;
this.data = {};
this.description = {};
this.data.dateAdded = new Date();
this.data.dateModified = new Date();
}
get id() {
return this._id;
}
setData(newData) {
this.data = Object.assign(Object.assign({}, this.data), newData);
return this;
}
setDescription(description) {
if (!utils_1.isInteger(description.languageId))
throw new Error(errors_1.default.NOT_SPECIFIED("description.language_id"));
this.description[description.languageId] = Object.assign({}, description);
return this;
}
insert() {
return __awaiter(this, void 0, void 0, function* () {
if (utils_1.isInteger(this._id))
throw new Error(errors_1.default.ENTITY_INSERTED);
const { insertId } = yield this.model.product.insert(this.data);
this._id = insertId;
yield this.model.product.toStore(this._id, 0);
const langIds = Object.keys(this.description).map(Number);
for (const langId of langIds) {
yield this.model.product.description.insert(Object.assign(Object.assign({}, this.description[langId]), { productId: this._id, languageId: langId }));
}
return this;
});
}
update() {
return __awaiter(this, void 0, void 0, function* () {
if (!utils_1.isInteger(this._id))
throw new Error(errors_1.default.ENTITY_NOT_INSERTED);
yield this.model.product.update({ productId: this._id }, Object.assign(Object.assign({}, this.data), { productId: this._id }));
const langIds = Object.keys(this.description).map(Number);
for (const langId of langIds) {
yield this.model.product.description.update({ productId: this._id, languageId: langId }, Object.assign(Object.assign({}, this.description[langId]), { productId: this._id, languageId: langId }));
}
return this;
});
}
delete() {
return __awaiter(this, void 0, void 0, function* () {
if (!utils_1.isInteger(this._id))
throw new Error(errors_1.default.ENTITY_NOT_INSERTED);
yield this.model.product.delete({ productId: this._id });
yield this.model.product.description.delete({ productId: this._id });
yield this.model.product.attribute.delete({ productId: this._id });
yield this.model.product.special.delete({ productId: this._id });
yield this.model.product.image.delete({ productId: this._id });
this._id = undefined;
return this;
});
}
toCategory(category, isMain = false) {
return __awaiter(this, void 0, void 0, function* () {
if (!utils_1.isInteger(this._id))
throw new Error(errors_1.default.ENTITY_NOT_INSERTED);
if (!utils_1.isInteger(category.id))
throw new Error(errors_1.default.NOT_SPECIFIED("category.id"));
yield this.model.product.toCategory(this._id, category.id, isMain ? 1 : 0);
return this;
});
}
setAttribute(attribute, data) {
return __awaiter(this, void 0, void 0, function* () {
if (!utils_1.isInteger(this._id))
throw new Error(errors_1.default.ENTITY_NOT_INSERTED);
if (!utils_1.isInteger(attribute.id))
throw new Error(errors_1.default.NOT_SPECIFIED("attribute.id"));
yield this.model.product.attribute.insert({
productId: this._id,
attributeId: attribute.id,
languageId: data.languageId,
text: data.text,
});
return this;
});
}
}
exports.default = Product;