diff --git a/app/locales/en.json b/app/locales/en.json index ed876d8..babe3e5 100644 --- a/app/locales/en.json +++ b/app/locales/en.json @@ -67,6 +67,7 @@ "tasks.recipes.setupGroup": "setup group profile and members", "tasks.recipes.createProfile": "create your profile", "tasks.steps.groupProfile": "enter your group profile information", + "tasks.steps.supplierProfile": "enter your supplier profile information", "tasks.steps.memberInvites": "invite group members", "tasks.recipes.setupSupplier": "setup supplier profile and products" } diff --git a/db/migrations/20170818142530_add-agent-type-ids-to-orders.js b/db/migrations/20170818142530_add-agent-type-ids-to-orders.js new file mode 100644 index 0000000..67b8d21 --- /dev/null +++ b/db/migrations/20170818142530_add-agent-type-ids-to-orders.js @@ -0,0 +1,13 @@ +exports.up = function(knex, Promise) { + return knex.schema.table('orders', function(table){ + table.renameColumn('agentId', 'consumerAgentId') + table.integer('supplierAgentId').references('agents.id') + }) +}; + +exports.down = function(knex, Promise) { + return knex.schema.table('orders', function(table){ + table.renameColumn('consumerAgentId', 'agentId') + table.dropColumn('supplierAgentId') + }) +}; diff --git a/ordering/services/orders.js b/ordering/services/orders.js index 3d04e04..881a372 100644 --- a/ordering/services/orders.js +++ b/ordering/services/orders.js @@ -1,6 +1,6 @@ const feathersKnex = require('feathers-knex') const { iff } = require('feathers-hooks-common') -import { pipe, equals, length, isNil } from 'ramda' +import { pipe, equals, length, isNil, isEmpty } from 'ramda' import * as taskRecipes from '../../tasks/data/recipes' module.exports = function () { @@ -17,7 +17,9 @@ module.exports = function () { const hooks = { before: { create: [ - iff(hasNoGroupAgent, createGroupAgent) + iff(hasNoGroupAgent, createGroupAgent), + iff(hasNoSupplierAgent, createSupplierAgent), + iff(hasNoRelation, createRelation) ] }, after: { @@ -32,21 +34,42 @@ function createGroupAgent (hook) { const agents = hook.app.service('agents') return agents.create({ type: 'group' }) .then((agent) => { - hook.data.agentId = agent.id + hook.data.consumerAgentId = agent.id return hook }) } function hasNoGroupAgent (hook) { - return isNil(hook.data.agentId) + return isNil(hook.data.consumerAgentId) +} + +function hasNoRelation (hook) { + const relationships = hook.app.service('relationships') + const supplierAgentId = hook.data.supplierAgentId + return relationships.find({ query: { sourceId: supplierAgentId } }).then((relationship)=>{ + return isEmpty(relationship) + }) +} + +function createRelation (hook){ + const relationships = hook.app.service('relationships') + const consumerAgentId = hook.data.consumerAgentId + const supplierAgentId = hook.data.supplierAgentId + return relationships.create({ + relationshipType: 'supplier', + sourceId: consumerAgentId, + targetId: supplierAgentId +}).then(() => { + return hook +}) } const hasLengthOne = pipe(length, equals(1)) function hasOneOrder (hook) { const orders = hook.app.service('orders') - const agentId = hook.data.agentId - return orders.find({ query: { agentId } }) + const consumerAgentId = hook.data.consumerAgentId + return orders.find({ query: { consumerAgentId } }) .then(hasLengthOne) } @@ -59,10 +82,24 @@ function createPrereqTaskPlan (hook) { const assigneeId = hook.params.credential.agentId const params = { - contextAgentId: hook.data.agentId + consumerAgentId: hook.data.consumerAgentId, + supplierAgentId: hook.data.supplierAgentId } return taskPlans.create({ taskRecipeId, params, assigneeId }) .then(() => { return hook }) } + +function createSupplierAgent (hook) { + const agents = hook.app.service('agents') + return agents.create({ type: 'group' }) + .then((agent) => { + hook.data.supplierAgentId = agent.id + return hook + }) +} + +function hasNoSupplierAgent (hook) { + return isNil(hook.data.supplierAgentId) +} diff --git a/tasks/components/SetupGroupTask.js b/tasks/components/SetupGroupTask.js index 56c1a6e..4aff49b 100644 --- a/tasks/components/SetupGroupTask.js +++ b/tasks/components/SetupGroupTask.js @@ -20,10 +20,10 @@ const rolesToRelationships = (roles = {}) => { export default (props) => { const { taskPlan, actions } = props if (isNil(taskPlan)) return null - const { params: { contextAgent } } = taskPlan - if (isNil(contextAgent)) return null + const { params: { consumerAgent } } = taskPlan + if (isNil(consumerAgent)) return null - const { profile, members } = contextAgent + const { profile, members } = consumerAgent console.log('members', members) @@ -33,14 +33,14 @@ export default (props) => { content: h(Profile, { initialValues: profile, updateProfile: (nextProfile) => { - actions.profiles.update(profile.id, merge(nextProfile, { agentId: contextAgent.id })) + actions.profiles.update(profile.id, merge(nextProfile, { agentId: consumerAgent.id })) } }) }, { id: 'tasks.steps.memberInvites', content: h(MemberInvites, { - agent: contextAgent, + agent: consumerAgent, initialValues: { members }, @@ -59,7 +59,7 @@ export default (props) => { credential = {}, } = agent const relationships = rolesToRelationships(roles) - const contextAgentId = contextAgent.id + const consumerAgentId = consumerAgent.id const agentData = { id, @@ -67,7 +67,7 @@ export default (props) => { profile, credential, relationships, - contextAgentId + consumerAgentId } if (isNil(agentData.id)) { diff --git a/tasks/components/SetupSupplierTask.js b/tasks/components/SetupSupplierTask.js index ecbc1de..bd1ea0b 100644 --- a/tasks/components/SetupSupplierTask.js +++ b/tasks/components/SetupSupplierTask.js @@ -1,6 +1,31 @@ -import React from 'react' +import h from 'react-hyperscript' +import { isNil, merge, isEmpty } from 'ramda' + +import TaskStepper from './TaskStepper' +import Profile from '../../agents/components/Profile' +import MemberInvites from '../../agents/components/MemberInvites' export default (props) => { - const { taskPlan } = props - return