Skip to content
1 change: 1 addition & 0 deletions app/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
13 changes: 13 additions & 0 deletions db/migrations/20170818142530_add-agent-type-ids-to-orders.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
exports.up = function(knex, Promise) {
return knex.schema.table('orders', function(table){
table.renameColumn('agentId', 'consumerAgentId')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice

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')
})
};
51 changes: 44 additions & 7 deletions ordering/services/orders.js
Original file line number Diff line number Diff line change
@@ -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 () {
Expand All @@ -17,7 +17,9 @@ module.exports = function () {
const hooks = {
before: {
create: [
iff(hasNoGroupAgent, createGroupAgent)
iff(hasNoGroupAgent, createGroupAgent),
iff(hasNoSupplierAgent, createSupplierAgent),
iff(hasNoRelation, createRelation)
]
},
after: {
Expand All @@ -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)
}

Expand All @@ -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)
}
14 changes: 7 additions & 7 deletions tasks/components/SetupGroupTask.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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
},
Expand All @@ -59,15 +59,15 @@ export default (props) => {
credential = {},
} = agent
const relationships = rolesToRelationships(roles)
const contextAgentId = contextAgent.id
const consumerAgentId = consumerAgent.id

const agentData = {
id,
type,
profile,
credential,
relationships,
contextAgentId
consumerAgentId
}

if (isNil(agentData.id)) {
Expand Down
31 changes: 28 additions & 3 deletions tasks/components/SetupSupplierTask.js
Original file line number Diff line number Diff line change
@@ -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 <div>Setup supplier yo!</div>
const { taskPlan, actions } = props
if (isNil(taskPlan)) return null
const { params: { supplierAgent } } = taskPlan
if (isNil(supplierAgent)) return null

const { profile, members } = supplierAgent

const steps = [
{
id: 'tasks.steps.supplierProfile',
content: h(Profile, {
initialValues: profile,
updateProfile: (nextProfile) => {
actions.profiles.update(profile.id, merge(nextProfile, { agentId: supplierAgent.id }))
}
})
},
]

return h(TaskStepper, {
steps
})
}
24 changes: 12 additions & 12 deletions tasks/containers/SetupGroupTask.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { agents, profiles, relationships, credentials } from 'dogstack-agents/ac
import getSetupGroupTaskProps from '../getters/getSetupGroupTaskProps'
import SetupGroupTask from '../components/SetupGroupTask'

const getContextAgentFromTaskPlan = path(['params', 'contextAgent'])
const getConsumerAgentFromTaskPlan = path(['params', 'consumerAgent'])

export default compose(
connectFeathers({
Expand All @@ -21,34 +21,34 @@ export default compose(
// new queries by checking if deepEqual
query: (props) => {
var queries = []
// once we have the task plan, query for the context agent
// once we have the task plan, query for the consumer agent
const { taskPlan } = props

if (taskPlan) {
const { params: { contextAgentId, contextAgent } } = taskPlan
const { params: { consumerAgentId, consumerAgent } } = taskPlan
queries.push({
service: 'agents',
id: contextAgentId
id: consumerAgentId
})
queries.push({
service: 'profiles',
params: {
query: {
agentId: contextAgentId
agentId: consumerAgentId
}
}
})
queries.push({
service: 'relationships',
params: {
query: {
sourceId: contextAgentId
sourceId: consumerAgentId
}
}
})

if (contextAgent) {
const { members } = contextAgent
if (consumerAgent) {
const { members } = consumerAgent
const queryEachMember = forEach(member => {
const { agentId } = member
queries.push({
Expand Down Expand Up @@ -86,11 +86,11 @@ export default compose(
// wait for task plan before re-query
if (isNil(taskPlan)) return false

// re-query when we haven't gotten back contextAgent or taskWork
const contextAgent = getContextAgentFromTaskPlan(taskPlan)
// re-query when we haven't gotten back consumerAgent or taskWork
const consumerAgent = getConsumerAgentFromTaskPlan(taskPlan)

if (isNil(contextAgent)) return true
if (anyMembersAreNil(contextAgent)) {
if (isNil(consumerAgent)) return true
if (anyMembersAreNil(consumerAgent)) {
return true
}

Expand Down
60 changes: 60 additions & 0 deletions tasks/containers/SetupSupplierTask.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { isNil, path, prop, pipe, values, any, forEach, either } from 'ramda'
import { connect as connectFeathers } from 'feathers-action-react'
import { compose } from 'recompose'

import { agents, profiles, relationships, credentials } from 'dogstack-agents/actions'
import getSetupGroupTaskProps from '../getters/getSetupGroupTaskProps'
import SetupSupplierTask from '../components/SetupSupplierTask'

const getSupplierAgentFromTaskPlan = path(['params', 'supplierAgent'])

export default compose(
connectFeathers({
selector: getSetupGroupTaskProps,
actions: {
agents,
profiles,
relationships,
credentials
},
// TODO can optimize `feathers-action-react` to de-dupe
// new queries by checking if deepEqual
query: (props) => {
var queries = []
// once we have the task plan, query for the supplier agent
const { taskPlan } = props

if (taskPlan) {
const { params: { supplierAgentId } } = taskPlan
queries.push({
service: 'agents',
id: supplierAgentId
})
queries.push({
service: 'profiles',
params: {
query: {
agentId: supplierAgentId
}
}
})
}

return queries
},
shouldQueryAgain: (props, status) => {
if (status.isPending) return false

const { taskPlan } = props.ownProps

// wait for task plan before re-query
if (isNil(taskPlan)) return false

// re-query when we haven't gotten back supplierAgent or taskWork
const supplierAgent = getSupplierAgentFromTaskPlan(taskPlan)
if (isNil(supplierAgent)) return true

return false
}
})
)(SetupSupplierTask)
2 changes: 1 addition & 1 deletion tasks/data/recipes.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import SetupGroupTask from '../containers/SetupGroupTask'
import SetupSupplierTask from '../components/SetupSupplierTask'
import SetupSupplierTask from '../containers/SetupSupplierTask'
import CreateProfileTask from '../containers/CreateProfileTask'

export const setupGroup = {
Expand Down
12 changes: 8 additions & 4 deletions tasks/getters/getEnhancedTaskPlans.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,16 @@ const getEnhancedTaskPlans = createSelector(
const assignee = agents[assigneeId]
const taskWork = filter(propEq('taskPlanId', id))(taskWorks)

const { contextAgentId } = params
const contextAgent = isNil(contextAgentId)
const { consumerAgentId, supplierAgentId } = params
const consumerAgent = isNil(consumerAgentId)
? null
: agents[contextAgentId]
: agents[consumerAgentId]
const supplierAgent = isNil(supplierAgentId)
? null
: agents[supplierAgentId]
const nextParams = merge(params, {
contextAgent
consumerAgent,
supplierAgent
})

return merge(taskPlan, {
Expand Down