medusa-plugin-ratings 1.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.
- package/LICENSE +21 -0
- package/README.md +28 -0
- package/dist/api/index.js +58 -0
- package/dist/migrations/1674838435739-ProductReview.js +29 -0
- package/dist/models/product-review.js +76 -0
- package/dist/repositories/product-review.js +6 -0
- package/dist/services/product-review.js +58 -0
- package/package.json +57 -0
- package/tsconfig.json +25 -0
- package/tsconfig.spec.json +5 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Lacey Pevey
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# medusa-plugin-ratings
|
|
2
|
+
|
|
3
|
+
Simple reviews and ratings for Medusajs
|
|
4
|
+
|
|
5
|
+
[Documentation](https://pevey.com/medusa-plugin-ratings)
|
|
6
|
+
|
|
7
|
+
If you are not familiar with Medusa, you can learn more on [the project web site](https://www.medusajs.com/).
|
|
8
|
+
|
|
9
|
+
> Medusa is a set of commerce modules and tools that allow you to build rich, reliable, and performant commerce applications without reinventing core commerce logic. The modules can be customized and used to build advanced ecommerce stores, marketplaces, or any product that needs foundational commerce primitives. All modules are open-source and freely available on npm.
|
|
10
|
+
|
|
11
|
+
## Features
|
|
12
|
+
|
|
13
|
+
- Adds review object and data repository
|
|
14
|
+
- Adds endpoints for retrieving reviews by product and by customer
|
|
15
|
+
- Adds endpoint for adding a review
|
|
16
|
+
- By default, reviews are set with field `approved` to `false` (boolean)
|
|
17
|
+
- Until Medusa adds extensibility for the admin app, approval must be done manually in the database.
|
|
18
|
+
|
|
19
|
+
## Configuration
|
|
20
|
+
|
|
21
|
+
Enable in your medusa-config.js file similar to other plugins:
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
const plugins = [
|
|
25
|
+
`medusa-plugin-ratings`,
|
|
26
|
+
...
|
|
27
|
+
]
|
|
28
|
+
```
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const express_1 = require("express");
|
|
7
|
+
const medusa_core_utils_1 = require("medusa-core-utils");
|
|
8
|
+
const cors_1 = __importDefault(require("cors"));
|
|
9
|
+
const body_parser_1 = __importDefault(require("body-parser"));
|
|
10
|
+
exports.default = (rootDirectory) => {
|
|
11
|
+
const { configModule } = (0, medusa_core_utils_1.getConfigFile)(rootDirectory, "medusa-config");
|
|
12
|
+
const { projectConfig } = configModule;
|
|
13
|
+
const storeCorsOptions = {
|
|
14
|
+
origin: projectConfig.store_cors.split(","),
|
|
15
|
+
credentials: true,
|
|
16
|
+
};
|
|
17
|
+
const adminCorsOptions = {
|
|
18
|
+
origin: projectConfig.admin_cors.split(","),
|
|
19
|
+
credentials: true,
|
|
20
|
+
};
|
|
21
|
+
const router = (0, express_1.Router)();
|
|
22
|
+
router.get("/store/products/:id/reviews", (0, cors_1.default)(storeCorsOptions), (req, res) => {
|
|
23
|
+
const productReviewService = req.scope.resolve("productReviewService");
|
|
24
|
+
productReviewService.getProductReviews(req.params.id).then((product_reviews) => {
|
|
25
|
+
return res.json({
|
|
26
|
+
product_reviews
|
|
27
|
+
});
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
router.get("/store/customers/:id/reviews", (0, cors_1.default)(storeCorsOptions), (req, res) => {
|
|
31
|
+
const productReviewService = req.scope.resolve("productReviewService");
|
|
32
|
+
productReviewService.getCustomerProductReviews(req.params.id).then((product_reviews) => {
|
|
33
|
+
return res.json({
|
|
34
|
+
product_reviews
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
router.use(body_parser_1.default.json());
|
|
39
|
+
router.options("/store/products/:id/reviews", (0, cors_1.default)(storeCorsOptions));
|
|
40
|
+
router.post("/store/products/:id/reviews", (0, cors_1.default)(storeCorsOptions), (req, res) => {
|
|
41
|
+
const productReviewService = req.scope.resolve("productReviewService");
|
|
42
|
+
productReviewService.addProductReview(req.params.id, req.body.customer_id, req.body.display_name, req.body.content, req.body.rating).then((product_review) => {
|
|
43
|
+
return res.json({
|
|
44
|
+
product_review
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
router.options("/admin/products/:id/reviews", (0, cors_1.default)(adminCorsOptions));
|
|
49
|
+
router.get("/admin/products/:id/reviews", (0, cors_1.default)(adminCorsOptions), async (req, res) => {
|
|
50
|
+
const productReviewService = req.scope.resolve("productReviewService");
|
|
51
|
+
productReviewService.getProductReviews(req.params.id).then((product_reviews) => {
|
|
52
|
+
return res.json({
|
|
53
|
+
product_reviews
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
return router;
|
|
58
|
+
};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ProductReview1674838435739 = void 0;
|
|
4
|
+
const typeorm_1 = require("typeorm");
|
|
5
|
+
class ProductReview1674838435739 {
|
|
6
|
+
async up(queryRunner) {
|
|
7
|
+
await queryRunner.query(`CREATE TABLE IF NOT EXISTS "product_review" ("id" character varying NOT NULL,
|
|
8
|
+
"product_id" character varying NOT NULL,
|
|
9
|
+
"customer_id" character varying NOT NULL,
|
|
10
|
+
"display_name" character varying NOT NULL,
|
|
11
|
+
"rating" integer NOT NULL,
|
|
12
|
+
"content" character varying NOT NULL,
|
|
13
|
+
"approved" boolean NOT NULL,
|
|
14
|
+
"created_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),
|
|
15
|
+
"updated_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now())`);
|
|
16
|
+
await queryRunner.createPrimaryKey("product_review", ["id"]);
|
|
17
|
+
await queryRunner.createForeignKey("product_review", new typeorm_1.TableForeignKey({
|
|
18
|
+
columnNames: ["product_id"],
|
|
19
|
+
referencedColumnNames: ["id"],
|
|
20
|
+
referencedTableName: "product",
|
|
21
|
+
onDelete: "CASCADE",
|
|
22
|
+
onUpdate: "CASCADE"
|
|
23
|
+
}));
|
|
24
|
+
}
|
|
25
|
+
async down(queryRunner) {
|
|
26
|
+
await queryRunner.dropTable("product_review", true);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
exports.ProductReview1674838435739 = ProductReview1674838435739;
|
|
@@ -0,0 +1,76 @@
|
|
|
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.ProductReview = void 0;
|
|
13
|
+
const medusa_1 = require("@medusajs/medusa");
|
|
14
|
+
const utils_1 = require("@medusajs/utils");
|
|
15
|
+
const typeorm_1 = require("typeorm");
|
|
16
|
+
const class_validator_1 = require("class-validator");
|
|
17
|
+
let ProductReview = class ProductReview extends utils_1.BaseEntity {
|
|
18
|
+
product_id;
|
|
19
|
+
product;
|
|
20
|
+
customer_id;
|
|
21
|
+
customer;
|
|
22
|
+
display_name;
|
|
23
|
+
rating;
|
|
24
|
+
content;
|
|
25
|
+
approved;
|
|
26
|
+
beforeInsert() {
|
|
27
|
+
this.id = (0, utils_1.generateEntityId)(this.id, "prev");
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
__decorate([
|
|
31
|
+
(0, typeorm_1.Index)(),
|
|
32
|
+
(0, typeorm_1.Column)({ type: "varchar", nullable: true }),
|
|
33
|
+
__metadata("design:type", String)
|
|
34
|
+
], ProductReview.prototype, "product_id", void 0);
|
|
35
|
+
__decorate([
|
|
36
|
+
(0, typeorm_1.ManyToOne)(() => medusa_1.Product),
|
|
37
|
+
(0, typeorm_1.JoinColumn)({ name: "product_id" }),
|
|
38
|
+
__metadata("design:type", medusa_1.Product)
|
|
39
|
+
], ProductReview.prototype, "product", void 0);
|
|
40
|
+
__decorate([
|
|
41
|
+
(0, typeorm_1.Column)({ type: "varchar", nullable: false }),
|
|
42
|
+
__metadata("design:type", String)
|
|
43
|
+
], ProductReview.prototype, "customer_id", void 0);
|
|
44
|
+
__decorate([
|
|
45
|
+
(0, typeorm_1.ManyToOne)(() => medusa_1.Product),
|
|
46
|
+
(0, typeorm_1.JoinColumn)({ name: "customer_id" }),
|
|
47
|
+
__metadata("design:type", medusa_1.Customer)
|
|
48
|
+
], ProductReview.prototype, "customer", void 0);
|
|
49
|
+
__decorate([
|
|
50
|
+
(0, typeorm_1.Column)({ type: "varchar", nullable: false }),
|
|
51
|
+
__metadata("design:type", String)
|
|
52
|
+
], ProductReview.prototype, "display_name", void 0);
|
|
53
|
+
__decorate([
|
|
54
|
+
(0, typeorm_1.Column)({ type: "int" }),
|
|
55
|
+
(0, class_validator_1.Min)(1),
|
|
56
|
+
(0, class_validator_1.Max)(5),
|
|
57
|
+
__metadata("design:type", Number)
|
|
58
|
+
], ProductReview.prototype, "rating", void 0);
|
|
59
|
+
__decorate([
|
|
60
|
+
(0, typeorm_1.Column)({ nullable: false }),
|
|
61
|
+
__metadata("design:type", String)
|
|
62
|
+
], ProductReview.prototype, "content", void 0);
|
|
63
|
+
__decorate([
|
|
64
|
+
(0, typeorm_1.Column)({ type: "boolean", nullable: false }),
|
|
65
|
+
__metadata("design:type", Boolean)
|
|
66
|
+
], ProductReview.prototype, "approved", void 0);
|
|
67
|
+
__decorate([
|
|
68
|
+
(0, typeorm_1.BeforeInsert)(),
|
|
69
|
+
__metadata("design:type", Function),
|
|
70
|
+
__metadata("design:paramtypes", []),
|
|
71
|
+
__metadata("design:returntype", void 0)
|
|
72
|
+
], ProductReview.prototype, "beforeInsert", null);
|
|
73
|
+
ProductReview = __decorate([
|
|
74
|
+
(0, typeorm_1.Entity)()
|
|
75
|
+
], ProductReview);
|
|
76
|
+
exports.ProductReview = ProductReview;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ProductReviewRepository = void 0;
|
|
4
|
+
const product_review_1 = require("../models/product-review");
|
|
5
|
+
const database_1 = require("@medusajs/medusa/dist/loaders/database");
|
|
6
|
+
exports.ProductReviewRepository = database_1.dataSource.getRepository(product_review_1.ProductReview);
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const utils_1 = require("@medusajs/utils");
|
|
4
|
+
class ProductReviewService extends utils_1.TransactionBaseService {
|
|
5
|
+
productReviewRepository_;
|
|
6
|
+
constructor({ productReviewRepository }) {
|
|
7
|
+
super(arguments[0]);
|
|
8
|
+
this.productReviewRepository_ = productReviewRepository;
|
|
9
|
+
}
|
|
10
|
+
async getProductReviews(product_id) {
|
|
11
|
+
const productReviewRepository = this.activeManager_.withRepository(this.productReviewRepository_);
|
|
12
|
+
return await productReviewRepository.find({
|
|
13
|
+
where: {
|
|
14
|
+
product_id,
|
|
15
|
+
approved: true
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
async getCustomerProductReviews(customer_id) {
|
|
20
|
+
const productReviewRepository = this.activeManager_.withRepository(this.productReviewRepository_);
|
|
21
|
+
return await productReviewRepository.find({
|
|
22
|
+
where: {
|
|
23
|
+
customer_id,
|
|
24
|
+
approved: true
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
async addProductReview(product_id, customer_id, display_name, content, rating) {
|
|
29
|
+
if (!product_id || !customer_id || !display_name || !content || !rating) {
|
|
30
|
+
throw new Error("product review requires product_id, customer_id, display_name, content, and rating");
|
|
31
|
+
}
|
|
32
|
+
const productReviewRepository = this.activeManager_.withRepository(this.productReviewRepository_);
|
|
33
|
+
const createdReview = productReviewRepository.create({
|
|
34
|
+
product_id: product_id,
|
|
35
|
+
customer_id: customer_id,
|
|
36
|
+
display_name: display_name,
|
|
37
|
+
content: content,
|
|
38
|
+
rating: rating,
|
|
39
|
+
approved: false
|
|
40
|
+
});
|
|
41
|
+
const productReview = await productReviewRepository.save(createdReview);
|
|
42
|
+
return productReview;
|
|
43
|
+
}
|
|
44
|
+
async editProductReview(id, display_name, content, rating, approved) {
|
|
45
|
+
if (!id || !display_name || !content || !rating || !approved) {
|
|
46
|
+
throw new Error("updating a product review requires id, display_name, content, rating, and approved");
|
|
47
|
+
}
|
|
48
|
+
const productReviewRepository = this.activeManager_.withRepository(this.productReviewRepository_);
|
|
49
|
+
const productReview = productReviewRepository.update(id, {
|
|
50
|
+
display_name: display_name,
|
|
51
|
+
content: content,
|
|
52
|
+
rating: rating,
|
|
53
|
+
approved: approved
|
|
54
|
+
});
|
|
55
|
+
return productReview;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
exports.default = ProductReviewService;
|
package/package.json
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "medusa-plugin-ratings",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Simple reviews and ratings for Medusajs",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "https://github.com/pevey/medusa-plugin-ratings"
|
|
8
|
+
},
|
|
9
|
+
"homepage": "https://pevey.com/medusa-plugin-ratings",
|
|
10
|
+
"author": "Lacey Pevey",
|
|
11
|
+
"license": "MIT",
|
|
12
|
+
"keywords": [
|
|
13
|
+
"reviews",
|
|
14
|
+
"feedback",
|
|
15
|
+
"ratings",
|
|
16
|
+
"ecommerce",
|
|
17
|
+
"headless",
|
|
18
|
+
"medusa"
|
|
19
|
+
],
|
|
20
|
+
"devDependencies": {
|
|
21
|
+
"@babel/cli": "^7.7.5",
|
|
22
|
+
"@babel/core": "^7.7.5",
|
|
23
|
+
"@babel/node": "^7.7.4",
|
|
24
|
+
"@babel/plugin-proposal-class-properties": "^7.7.4",
|
|
25
|
+
"@babel/plugin-transform-instanceof": "^7.8.3",
|
|
26
|
+
"@babel/plugin-transform-runtime": "^7.7.6",
|
|
27
|
+
"@babel/preset-env": "^7.7.5",
|
|
28
|
+
"@babel/register": "^7.7.4",
|
|
29
|
+
"@babel/runtime": "^7.9.6",
|
|
30
|
+
"awilix": "^8.0.0",
|
|
31
|
+
"client-sessions": "^0.8.0",
|
|
32
|
+
"cross-env": "^5.2.1",
|
|
33
|
+
"jest": "^25.5.2",
|
|
34
|
+
"medusa-core-utils": "^1.2.0",
|
|
35
|
+
"medusa-test-utils": "^1.1.40"
|
|
36
|
+
},
|
|
37
|
+
"scripts": {
|
|
38
|
+
"prepare": "cross-env NODE_ENV=production yarn run build",
|
|
39
|
+
"test": "jest --passWithNoTests src",
|
|
40
|
+
"build": "babel src --out-dir . --ignore '**/__tests__','**/__mocks__'",
|
|
41
|
+
"watch": "babel -w src --out-dir . --ignore '**/__tests__','**/__mocks__'"
|
|
42
|
+
},
|
|
43
|
+
"peerDependencies": {
|
|
44
|
+
"@medusajs/medusa": "^1.10.0",
|
|
45
|
+
"medusa-interfaces": "1.3.7",
|
|
46
|
+
"typeorm": "npm:@medusajs/typeorm@next",
|
|
47
|
+
"@medusajs/utils": "1.8.3"
|
|
48
|
+
},
|
|
49
|
+
"dependencies": {
|
|
50
|
+
"@babel/plugin-transform-classes": "^7.9.5",
|
|
51
|
+
"body-parser": "^1.19.0",
|
|
52
|
+
"class-validator": "^0.14.0",
|
|
53
|
+
"cors": "^2.8.5",
|
|
54
|
+
"express": "^4.17.1",
|
|
55
|
+
"zod": "^3.21.4"
|
|
56
|
+
}
|
|
57
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"lib": ["es5", "es6"],
|
|
4
|
+
"target": "esnext",
|
|
5
|
+
"allowJs": true,
|
|
6
|
+
"esModuleInterop": true,
|
|
7
|
+
"module": "commonjs",
|
|
8
|
+
"moduleResolution": "node",
|
|
9
|
+
"emitDecoratorMetadata": true,
|
|
10
|
+
"experimentalDecorators": true,
|
|
11
|
+
"skipLibCheck": true,
|
|
12
|
+
"skipDefaultLibCheck": true,
|
|
13
|
+
"declaration": false,
|
|
14
|
+
"sourceMap": false,
|
|
15
|
+
"outDir": "./dist",
|
|
16
|
+
"rootDir": "src",
|
|
17
|
+
"baseUrl": "src"
|
|
18
|
+
},
|
|
19
|
+
"include": ["src"],
|
|
20
|
+
"exclude": [
|
|
21
|
+
"**/__tests__",
|
|
22
|
+
"**/__fixtures__",
|
|
23
|
+
"node_modules"
|
|
24
|
+
]
|
|
25
|
+
}
|