medusa-plugin-product-reviews-kvy
Version:
Plugins Product Reviews for Medusa ecommerce server.
464 lines (463 loc) • 20 kB
JavaScript
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
var _medusa = require("@medusajs/medusa");
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty2["default"])(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = (0, _getPrototypeOf2["default"])(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = (0, _getPrototypeOf2["default"])(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2["default"])(this, result); }; }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
var ProductReviewService = exports["default"] = /*#__PURE__*/function (_TransactionBaseServi) {
(0, _inherits2["default"])(ProductReviewService, _TransactionBaseServi);
var _super = _createSuper(ProductReviewService);
function ProductReviewService(_ref, config) {
var _this;
var productReviewRepository = _ref.productReviewRepository,
orderRepository = _ref.orderRepository,
customerRepository = _ref.customerRepository,
manager = _ref.manager;
(0, _classCallCheck2["default"])(this, ProductReviewService);
// @ts-ignore
_this = _super.apply(this, arguments);
_this.config = config;
_this.productReviewRepository = productReviewRepository;
_this.orderRepository = orderRepository;
_this.customerRepository = customerRepository;
_this.manager = manager;
return _this;
}
(0, _createClass2["default"])(ProductReviewService, [{
key: "getReviews",
value: function () {
var _getReviews = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(product_id) {
var approved,
page,
limit,
productReviewRepository,
_yield$productReviewR,
_yield$productReviewR2,
reviews,
total,
_args = arguments;
return _regenerator["default"].wrap(function _callee$(_context) {
while (1) switch (_context.prev = _context.next) {
case 0:
approved = _args.length > 1 && _args[1] !== undefined ? _args[1] : true;
page = _args.length > 2 && _args[2] !== undefined ? _args[2] : 1;
limit = _args.length > 3 && _args[3] !== undefined ? _args[3] : 10;
productReviewRepository = this.manager.getCustomRepository(this.productReviewRepository);
_context.next = 6;
return productReviewRepository.findAndCount({
where: _objectSpread({
product: {
id: product_id
}
}, approved !== "all" && {
approved: approved
}),
skip: (page - 1) * limit,
take: limit,
relations: ["order", "product", "order.items"]
});
case 6:
_yield$productReviewR = _context.sent;
_yield$productReviewR2 = (0, _slicedToArray2["default"])(_yield$productReviewR, 2);
reviews = _yield$productReviewR2[0];
total = _yield$productReviewR2[1];
return _context.abrupt("return", {
reviews: reviews,
total: total
});
case 11:
case "end":
return _context.stop();
}
}, _callee, this);
}));
function getReviews(_x) {
return _getReviews.apply(this, arguments);
}
return getReviews;
}()
}, {
key: "getAndCountAverageReviewsProduct",
value: function () {
var _getAndCountAverageReviewsProduct = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee2(product_id) {
var approved,
productReviewRepository,
_yield$productReviewR3,
_yield$productReviewR4,
reviews,
total,
average,
_args2 = arguments;
return _regenerator["default"].wrap(function _callee2$(_context2) {
while (1) switch (_context2.prev = _context2.next) {
case 0:
approved = _args2.length > 1 && _args2[1] !== undefined ? _args2[1] : true;
productReviewRepository = this.manager.getCustomRepository(this.productReviewRepository);
_context2.next = 4;
return productReviewRepository.findAndCount({
where: _objectSpread({
product: {
id: product_id
}
}, approved !== "all" && {
approved: approved
})
});
case 4:
_yield$productReviewR3 = _context2.sent;
_yield$productReviewR4 = (0, _slicedToArray2["default"])(_yield$productReviewR3, 2);
reviews = _yield$productReviewR4[0];
total = _yield$productReviewR4[1];
average = reviews.reduce(function (sum, r) {
return sum += r.stars;
}, 0) / reviews.length;
return _context2.abrupt("return", {
reviews: reviews,
total: total,
average: average
});
case 10:
case "end":
return _context2.stop();
}
}, _callee2, this);
}));
function getAndCountAverageReviewsProduct(_x2) {
return _getAndCountAverageReviewsProduct.apply(this, arguments);
}
return getAndCountAverageReviewsProduct;
}()
}, {
key: "getReviewsByCustomer",
value: function () {
var _getReviewsByCustomer = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee3(customer_id) {
var approved,
productReviewRepository,
_args3 = arguments;
return _regenerator["default"].wrap(function _callee3$(_context3) {
while (1) switch (_context3.prev = _context3.next) {
case 0:
approved = _args3.length > 1 && _args3[1] !== undefined ? _args3[1] : true;
productReviewRepository = this.manager.getCustomRepository(this.productReviewRepository);
_context3.next = 4;
return productReviewRepository.find({
where: {
customer: {
id: customer_id
},
approved: approved
}
});
case 4:
return _context3.abrupt("return", _context3.sent);
case 5:
case "end":
return _context3.stop();
}
}, _callee3, this);
}));
function getReviewsByCustomer(_x3) {
return _getReviewsByCustomer.apply(this, arguments);
}
return getReviewsByCustomer;
}()
}, {
key: "getReviewsByOrder",
value: function () {
var _getReviewsByOrder = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee4(conditions) {
var productReviewRepository;
return _regenerator["default"].wrap(function _callee4$(_context4) {
while (1) switch (_context4.prev = _context4.next) {
case 0:
productReviewRepository = this.manager.getCustomRepository(this.productReviewRepository);
_context4.next = 3;
return productReviewRepository.find({
where: _objectSpread({}, conditions),
relations: ["order", "product"]
});
case 3:
return _context4.abrupt("return", _context4.sent);
case 4:
case "end":
return _context4.stop();
}
}, _callee4, this);
}));
function getReviewsByOrder(_x4) {
return _getReviewsByOrder.apply(this, arguments);
}
return getReviewsByOrder;
}()
}, {
key: "getCustomerProductReviewByProduct",
value: function () {
var _getCustomerProductReviewByProduct = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee5(_ref2) {
var customer_id, product_id, productReviewRepository;
return _regenerator["default"].wrap(function _callee5$(_context5) {
while (1) switch (_context5.prev = _context5.next) {
case 0:
customer_id = _ref2.customer_id, product_id = _ref2.product_id;
productReviewRepository = this.manager.getCustomRepository(this.productReviewRepository);
_context5.next = 4;
return productReviewRepository.findOne({
where: {
customer: {
id: customer_id
},
product: {
id: product_id
}
}
});
case 4:
return _context5.abrupt("return", _context5.sent);
case 5:
case "end":
return _context5.stop();
}
}, _callee5, this);
}));
function getCustomerProductReviewByProduct(_x5) {
return _getCustomerProductReviewByProduct.apply(this, arguments);
}
return getCustomerProductReviewByProduct;
}()
}, {
key: "getProductOrder",
value: function () {
var _getProductOrder = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee6(_ref3) {
var customer_id, product_id, orderRepository, orders, order;
return _regenerator["default"].wrap(function _callee6$(_context6) {
while (1) switch (_context6.prev = _context6.next) {
case 0:
customer_id = _ref3.customer_id, product_id = _ref3.product_id;
orderRepository = this.manager.getCustomRepository(this.orderRepository); // Get all orders that are completed of the customer
_context6.next = 4;
return orderRepository.find({
where: {
customer_id: customer_id,
status: "completed"
},
relations: ["items"]
});
case 4:
orders = _context6.sent;
if (orders) {
_context6.next = 7;
break;
}
throw new Error("There are no orders for this customer");
case 7:
// Find the order that contains the product
order = orders.find(function (order) {
return order.items.some(function (item) {
return item.variant.product_id === product_id;
});
});
if (order) {
_context6.next = 10;
break;
}
throw new Error("There are no orders for this product");
case 10:
return _context6.abrupt("return", order);
case 11:
case "end":
return _context6.stop();
}
}, _callee6, this);
}));
function getProductOrder(_x6) {
return _getProductOrder.apply(this, arguments);
}
return getProductOrder;
}()
}, {
key: "addReview",
value: function () {
var _addReview = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee7(_ref4) {
var productId, customerId, orderId, name, content, stars, medias, productReviewRepository, createdReview, productReview;
return _regenerator["default"].wrap(function _callee7$(_context7) {
while (1) switch (_context7.prev = _context7.next) {
case 0:
productId = _ref4.productId, customerId = _ref4.customerId, orderId = _ref4.orderId, name = _ref4.name, content = _ref4.content, stars = _ref4.stars, medias = _ref4.medias;
productReviewRepository = this.manager.getCustomRepository(this.productReviewRepository);
createdReview = productReviewRepository.create({
product_id: productId,
customer_id: customerId,
order_id: orderId,
name: name,
medias: medias,
stars: stars,
content: content,
approved: false
});
_context7.next = 5;
return productReviewRepository.save(createdReview);
case 5:
productReview = _context7.sent;
return _context7.abrupt("return", productReview);
case 7:
case "end":
return _context7.stop();
}
}, _callee7, this);
}));
function addReview(_x7) {
return _addReview.apply(this, arguments);
}
return addReview;
}()
}, {
key: "updateProductReview",
value: function () {
var _updateProductReview = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee8(review_id, display_name, content, stars, medias) {
var productReviewRepository, condition, review;
return _regenerator["default"].wrap(function _callee8$(_context8) {
while (1) switch (_context8.prev = _context8.next) {
case 0:
if (review_id) {
_context8.next = 2;
break;
}
throw new Error("Missing review_id");
case 2:
productReviewRepository = this.manager.getCustomRepository(this.productReviewRepository);
condition = {
where: {
id: review_id
}
};
_context8.next = 6;
return productReviewRepository.findOne(condition);
case 6:
review = _context8.sent;
if (review) {
_context8.next = 9;
break;
}
throw new Error("Review not found");
case 9:
_context8.next = 11;
return productReviewRepository.update({
id: review.id
}, {
name: display_name,
medias: medias,
stars: stars,
content: content
});
case 11:
return _context8.abrupt("return", productReviewRepository.findOne(condition));
case 12:
case "end":
return _context8.stop();
}
}, _callee8, this);
}));
function updateProductReview(_x8, _x9, _x10, _x11, _x12) {
return _updateProductReview.apply(this, arguments);
}
return updateProductReview;
}()
}, {
key: "approveProductReview",
value: function () {
var _approveProductReview = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee9(_ref5) {
var review_id, _ref5$approved, approved, productReviewRepository;
return _regenerator["default"].wrap(function _callee9$(_context9) {
while (1) switch (_context9.prev = _context9.next) {
case 0:
review_id = _ref5.review_id, _ref5$approved = _ref5.approved, approved = _ref5$approved === void 0 ? true : _ref5$approved;
if (review_id) {
_context9.next = 3;
break;
}
throw new Error("Missing review_id");
case 3:
productReviewRepository = this.manager.getCustomRepository(this.productReviewRepository);
_context9.next = 6;
return productReviewRepository.update({
id: review_id
}, {
approved: approved
});
case 6:
return _context9.abrupt("return", productReviewRepository.findOne({
id: review_id
}));
case 7:
case "end":
return _context9.stop();
}
}, _callee9, this);
}));
function approveProductReview(_x13) {
return _approveProductReview.apply(this, arguments);
}
return approveProductReview;
}()
}, {
key: "deleteProductReview",
value: function () {
var _deleteProductReview = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee10(review_id) {
var productReviewRepository, condition, review;
return _regenerator["default"].wrap(function _callee10$(_context10) {
while (1) switch (_context10.prev = _context10.next) {
case 0:
if (review_id) {
_context10.next = 2;
break;
}
throw new Error("Missing review_id");
case 2:
productReviewRepository = this.manager.getCustomRepository(this.productReviewRepository);
condition = {
where: {
id: review_id
}
};
_context10.next = 6;
return productReviewRepository.findOne(condition);
case 6:
review = _context10.sent;
if (review) {
_context10.next = 9;
break;
}
throw new Error("Review not found");
case 9:
_context10.next = 11;
return productReviewRepository.softDelete({
id: review.id
});
case 11:
return _context10.abrupt("return", review);
case 12:
case "end":
return _context10.stop();
}
}, _callee10, this);
}));
function deleteProductReview(_x14) {
return _deleteProductReview.apply(this, arguments);
}
return deleteProductReview;
}()
}]);
return ProductReviewService;
}(_medusa.TransactionBaseService);