From 31243a64ca6e2ce48fd0fc538a5f8a45e12005fd Mon Sep 17 00:00:00 2001 From: trojanh Date: Wed, 7 Aug 2019 16:44:50 +0530 Subject: [PATCH 1/2] Update to reaction v2 --- register.js | 43 +++++---- server/init.js | 4 +- server/lib/collections/collections.js | 9 -- server/lib/collections/schemas/index.js | 1 - server/lib/collections/schemas/wishlist.js | 44 --------- server/lib/collections/wishlist.js | 43 +++++++++ server/wishlist-functions.js | 104 ++++++++++----------- 7 files changed, 114 insertions(+), 134 deletions(-) delete mode 100644 server/lib/collections/collections.js delete mode 100644 server/lib/collections/schemas/index.js delete mode 100644 server/lib/collections/schemas/wishlist.js create mode 100644 server/lib/collections/wishlist.js diff --git a/register.js b/register.js index e41818b8b..be3ac1129 100644 --- a/register.js +++ b/register.js @@ -1,24 +1,23 @@ -import { Reaction } from '/server/api'; +import Reaction from "/imports/plugins/core/core/server/Reaction"; Reaction.registerPackage({ - label: 'Classic WishList functionality for reaction', - name: 'reaction-wish-list', - icon: 'fa fa-thumb-tack', - autoEnable: true, - settings: { - }, - registry: [ - { - provides: 'dashboard', - label: 'Wishlist', - description: 'Classic wish list functionality', - route: '/dashboard/wishlist', - icon: 'fa fa-thumb-tack', - container: 'core', - template: 'wishlist', - name: 'dashboardProductImporter', - workflow: 'coreWorkflow', - priority: 2 - } - ] -}) \ No newline at end of file + label: "Classic WishList functionality for reaction", + name: "reaction-wish-list", + icon: "fa fa-thumb-tack", + autoEnable: true, + settings: {}, + registry: [ + { + container: "core", + description: "Classic wish list functionality", + icon: "fa fa-thumb-tack", + label: "Wishlist", + name: "dashboardProductImporter", + priority: 2, + provides: "dashboard", + route: "/dashboard/wishlist", + template: "wishlist", + workflow: "coreWorkflow" + } + ] +}); diff --git a/server/init.js b/server/init.js index 2390cab45..7556ff8a1 100644 --- a/server/init.js +++ b/server/init.js @@ -1,6 +1,6 @@ +import Logger from "@reactioncommerce/logger"; import { Meteor } from "meteor/meteor"; -import { Wishlist } from "./lib/collections"; -import { Hooks, Reaction, Logger } from "/server/api"; +import { Wishlist } from "/lib/collections"; Meteor.users.after.insert(function (userId, doc) { if ( doc.emails.length != 0 ) { diff --git a/server/lib/collections/collections.js b/server/lib/collections/collections.js deleted file mode 100644 index 266ef3b12..000000000 --- a/server/lib/collections/collections.js +++ /dev/null @@ -1,9 +0,0 @@ -import { Mongo } from "meteor/mongo"; -import * as Schemas from "./schemas"; - -export const Wishlist = new Mongo.Collection("Wishlist"); - -Wishlist.attachSchema([ - Schemas.Wishlist, - Schemas.WishlistItem -]); diff --git a/server/lib/collections/schemas/index.js b/server/lib/collections/schemas/index.js deleted file mode 100644 index 252dab9dd..000000000 --- a/server/lib/collections/schemas/index.js +++ /dev/null @@ -1 +0,0 @@ -export * from "./wishlist"; diff --git a/server/lib/collections/schemas/wishlist.js b/server/lib/collections/schemas/wishlist.js deleted file mode 100644 index 0ac59d533..000000000 --- a/server/lib/collections/schemas/wishlist.js +++ /dev/null @@ -1,44 +0,0 @@ -import { SimpleSchema } from "meteor/aldeed:simple-schema"; - -/** -* OrderItems Schema -* merges with Cart and Order to create Orders collection -*/ -export const WishListItem = new SimpleSchema({ - productId: { - type: String, - optional: false - }, - variantId: { - type: String, - optional: true - }, - title: { // Why do we want this? - type: String, - defaultValue: "", - label: "Product Title" - } - -}); - - - -/** - * Order Schema - */ -export const Wishlist = new SimpleSchema({ - - Id: { - type: String, - optional: true - }, - userId: { - type: String, - unique:true - }, - items: { - type: [WishListItem], - optional: true - } - -}); diff --git a/server/lib/collections/wishlist.js b/server/lib/collections/wishlist.js new file mode 100644 index 000000000..8709798ec --- /dev/null +++ b/server/lib/collections/wishlist.js @@ -0,0 +1,43 @@ +import SimpleSchema from "simpl-schema"; + +/** + * OrderItems Schema + * merges with Cart and Order to create Orders collection + */ +export const WishListItem = new SimpleSchema({ + productId: { + type: String, + optional: false + }, + variantId: { + type: String, + optional: true + }, + title: { + // Why do we want this? + type: String, + defaultValue: "", + label: "Product Title" + } +}); + +/** + * Order Schema + */ +export const Wishlist = new SimpleSchema({ + Id: { + type: String, + optional: true + }, + userId: { + type: String, + unique: true + }, + items: { + type: [WishListItem], + optional: true + } +}); + +registerSchema("Wishlist", Wishlist); +registerSchema("WishListItem", WishListItem); diff --git a/server/wishlist-functions.js b/server/wishlist-functions.js index c1b94fac2..03696bc76 100644 --- a/server/wishlist-functions.js +++ b/server/wishlist-functions.js @@ -1,74 +1,66 @@ -import {Meteor} from "meteor/meteor"; -import * as Collections from "/lib/collections"; -import {Reaction} from "/server/api"; -import {Products, Accounts} from "/lib/collections"; -import {Wishlist} from "./lib/collections" -import { check } from 'meteor/check' -//import * as Schemas from "/lib/collections/schemas - - -export function getWishlist(userId){ - const wishlistId=Wishlist.insert({userId: userId}); - return Wishlist.findOne({userId: userId}) +import { Meteor } from "meteor/meteor"; +import { Wishlist } from "/lib/collections"; +import { check } from "meteor/check"; +export function getWishlist(userId) { + return Wishlist.findOne({ userId }); } -export function createWishlist(userId){ - check(userId, String) - const wishlistId=Wishlist.insert({userId:userId}); - return Wishlist.findOne({_id: wishlistId}) +export function createWishlist(userId) { + check(userId, String); + const wishlistId = Wishlist.insert({ userId }); + return Wishlist.findOne({ _id: wishlistId }); } +export function addToWishlist(userId, product) { + let aWishlist = Wishlist.findOne({ userId }); + if (aWishlist == undefined) aWishlist = createWishlist(userId); -export function addToWishlist(userId, product){ - - var aWishlist = Wishlist.findOne({userId: userId}); - if(aWishlist == undefined) - aWishlist=createWishlist(userId); - - return Wishlist.update({ - _id: aWishlist._id - }, { - $push: { - items: { - "_id": Random.id(), - "productId": product._id, - "title": product.title, + return Wishlist.update( + { + _id: aWishlist._id + }, + { + $push: { + items: { + _id: Random.id(), + productId: product._id, + title: product.title + } } } - }); + ); } - -export function removeFromWishlist(userId, product, variantId){ - var aWishlist = Wishlist.findOne({userId: userId}); - return Wishlist.update({ - _id: aWishlist._id - }, { - $pull: { - items: { - productId: product._id - } +export function removeFromWishlist(userId, product, variantId) { + const aWishlist = Wishlist.findOne({ userId }); + return Wishlist.update( + { + _id: aWishlist._id + }, + { + $pull: { + items: { + productId: product._id } - }); - - + } + } + ); } Meteor.methods({ - "createWishlist": function(userId){ - check(userId,String); + createWishlist(userId) { + check(userId, String); createWishlist(userId); }, - "addToWishlist": function(userId,product){ - check(userId,String); - check(product,Object); - addToWishlist(userId,product); + addToWishlist(userId, product) { + check(userId, String); + check(product, Object); + addToWishlist(userId, product); }, - "removeFromWishlist": function(userId,product){ - check(userId,String); - check(product,Object); - removeFromWishlist(userId,product); - } - + removeFromWishlist(userId, product) { + check(userId, String); + check(product, Object); + removeFromWishlist(userId, product); + } }); From 11996988547eb0f536419f7d7820ae64ffa26a4d Mon Sep 17 00:00:00 2001 From: trojanh Date: Wed, 7 Aug 2019 21:01:24 +0530 Subject: [PATCH 2/2] Use reaction v2 conventions --- client/components/wishlist.js | 12 +++++----- lib/collections/index.js | 2 ++ {server/lib => lib}/collections/wishlist.js | 7 ++++-- server/index.js | 6 +++-- server/lib/collections/index.js | 2 -- server/{ => methods}/init.js | 7 +++--- .../wishlist-collection.app-test.js | 0 server/methods/wishlist.js | 9 ++++++++ .../wishlistMethods.js} | 22 +++++-------------- server/no-meteor/schema.graphql | 0 10 files changed, 36 insertions(+), 31 deletions(-) create mode 100644 lib/collections/index.js rename {server/lib => lib}/collections/wishlist.js (84%) delete mode 100644 server/lib/collections/index.js rename server/{ => methods}/init.js (66%) rename server/{ => methods}/wishlist-collection.app-test.js (100%) create mode 100644 server/methods/wishlist.js rename server/{wishlist-functions.js => methods/wishlistMethods.js} (73%) create mode 100644 server/no-meteor/schema.graphql diff --git a/client/components/wishlist.js b/client/components/wishlist.js index 2fb4e5f2d..62fa504fc 100644 --- a/client/components/wishlist.js +++ b/client/components/wishlist.js @@ -59,12 +59,12 @@ class Wishlist extends Component { } } -Wishlist.propTypes = { - variant: PropTypes.object, - isWished: PropTypes.bool, - user: PropTypes.object, - onWishlistClick: PropTypes.func -} +// Wishlist.propTypes = { +// variant: PropTypes.object, +// isWished: PropTypes.bool, +// user: PropTypes.object, +// onWishlistClick: PropTypes.func +// } registerComponent({ name: "Wishlist", diff --git a/lib/collections/index.js b/lib/collections/index.js new file mode 100644 index 000000000..541412416 --- /dev/null +++ b/lib/collections/index.js @@ -0,0 +1,2 @@ +export * from "./wishlist"; + diff --git a/server/lib/collections/wishlist.js b/lib/collections/wishlist.js similarity index 84% rename from server/lib/collections/wishlist.js rename to lib/collections/wishlist.js index 8709798ec..587030f6d 100644 --- a/server/lib/collections/wishlist.js +++ b/lib/collections/wishlist.js @@ -1,4 +1,5 @@ import SimpleSchema from "simpl-schema"; +import { registerSchema } from "@reactioncommerce/schemas"; /** * OrderItems Schema @@ -34,8 +35,10 @@ export const Wishlist = new SimpleSchema({ unique: true }, items: { - type: [WishListItem], - optional: true + type: Array + }, + "items.$": { + type: WishListItem, } }); diff --git a/server/index.js b/server/index.js index f1d2772b1..9fe4c3343 100644 --- a/server/index.js +++ b/server/index.js @@ -1,2 +1,4 @@ -import './lib/collections'; -import "./init"; +import "../lib/collections"; +import "./methods/wishlist"; +import "./methods/init"; + diff --git a/server/lib/collections/index.js b/server/lib/collections/index.js deleted file mode 100644 index e35f2b7fd..000000000 --- a/server/lib/collections/index.js +++ /dev/null @@ -1,2 +0,0 @@ -export * from "./collections"; - diff --git a/server/init.js b/server/methods/init.js similarity index 66% rename from server/init.js rename to server/methods/init.js index 7556ff8a1..65b347e91 100644 --- a/server/init.js +++ b/server/methods/init.js @@ -1,10 +1,11 @@ import Logger from "@reactioncommerce/logger"; -import { Meteor } from "meteor/meteor"; import { Wishlist } from "/lib/collections"; +import Hooks from "@reactioncommerce/hooks"; -Meteor.users.after.insert(function (userId, doc) { + +Hooks.Events.run("afterAccountsInsert", (function (userId, doc) { if ( doc.emails.length != 0 ) { Logger.info("::: Creating wishlist for user: #" + doc._id); Wishlist.insert({ userId: doc._id }) } -}); +})); diff --git a/server/wishlist-collection.app-test.js b/server/methods/wishlist-collection.app-test.js similarity index 100% rename from server/wishlist-collection.app-test.js rename to server/methods/wishlist-collection.app-test.js diff --git a/server/methods/wishlist.js b/server/methods/wishlist.js new file mode 100644 index 000000000..81ad5a3b9 --- /dev/null +++ b/server/methods/wishlist.js @@ -0,0 +1,9 @@ +import { Meteor } from "meteor/meteor"; +import * as WishlistMethods from "./wishlistMethods"; + +Meteor.methods({ + createWishlist: WishlistMethods.createWishlist, + addToWishlist: WishlistMethods.addToWishlist, + removeFromWishlist: WishlistMethods.removeFromWishlist, + getWishList: WishlistMethods.getWishlist +}); diff --git a/server/wishlist-functions.js b/server/methods/wishlistMethods.js similarity index 73% rename from server/wishlist-functions.js rename to server/methods/wishlistMethods.js index 03696bc76..30273116d 100644 --- a/server/wishlist-functions.js +++ b/server/methods/wishlistMethods.js @@ -13,6 +13,8 @@ export function createWishlist(userId) { } export function addToWishlist(userId, product) { + check(userId, String); + check(product, Object); let aWishlist = Wishlist.findOne({ userId }); if (aWishlist == undefined) aWishlist = createWishlist(userId); @@ -33,6 +35,9 @@ export function addToWishlist(userId, product) { } export function removeFromWishlist(userId, product, variantId) { + check(userId, String); + check(product, Object); + const aWishlist = Wishlist.findOne({ userId }); return Wishlist.update( { @@ -48,19 +53,4 @@ export function removeFromWishlist(userId, product, variantId) { ); } -Meteor.methods({ - createWishlist(userId) { - check(userId, String); - createWishlist(userId); - }, - addToWishlist(userId, product) { - check(userId, String); - check(product, Object); - addToWishlist(userId, product); - }, - removeFromWishlist(userId, product) { - check(userId, String); - check(product, Object); - removeFromWishlist(userId, product); - } -}); + diff --git a/server/no-meteor/schema.graphql b/server/no-meteor/schema.graphql new file mode 100644 index 000000000..e69de29bb