Skip to content

Commit

Permalink
Merge branch 'benhiller-creator_id' into stage-main
Browse files Browse the repository at this point in the history
  • Loading branch information
shakalee14 committed Apr 11, 2019
2 parents 8fee508 + 2cb5e85 commit ad76560
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 6 deletions.
63 changes: 63 additions & 0 deletions __test__/containers/CampaignList.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
* @jest-environment jsdom
*/
import React from 'react'
import { mount } from 'enzyme'
import MuiThemeProvider from 'material-ui/styles/MuiThemeProvider'
import { CampaignList } from '../../src/containers/CampaignList'

describe('Campaign list for campaign with null creator', () => {
// given
const campaignWithoutCreator = {
id: 1,
title: 'Yes on A',
creator: null,
}

const data = {
organization: {
campaigns: {
campaigns: [ campaignWithoutCreator ],
},
},
}

// when
test('Renders for campaign with null creator, doesn\'t include created by', () => {
const wrapper = mount(
<MuiThemeProvider>
<CampaignList data={data} />
</MuiThemeProvider>
)
expect(wrapper.text().includes('Created by')).toBeFalsy()
})
})

describe('Campaign list for campaign with creator', () => {
// given
const campaignWithCreator = {
id: 1,
creator: {
displayName: 'Lorem Ipsum'
},
}

const data = {
organization: {
campaigns: {
campaigns: [ campaignWithCreator ],
},
},
}

// when
test('Renders for campaign with creator, includes created by', () => {
const wrapper = mount(
<MuiThemeProvider>
<CampaignList data={data} />
</MuiThemeProvider>
)
expect(wrapper.containsMatchingElement(<span> &mdash; Created by Lorem Ipsum</span>)).toBeTruthy()
})
})

5 changes: 3 additions & 2 deletions src/api/campaign.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export const schema = `
dueBy: Date
isStarted: Boolean
isArchived: Boolean
creator: User
texters: [User]
assignments(assignmentsFilter: AssignmentsFilter): [Assignment]
interactionSteps: [InteractionStep]
Expand Down Expand Up @@ -57,9 +58,9 @@ export const schema = `
}
union CampaignsReturn = PaginatedCampaigns | CampaignsList
type PaginatedCampaigns {
campaigns: [Campaign]
pageInfo: PageInfo
}
}
`
8 changes: 7 additions & 1 deletion src/containers/CampaignList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ const campaignInfoFragment = `
hasUnsentInitialMessages
description
dueBy
creator {
displayName
}
`

const inlineStyles = {
Expand All @@ -42,7 +45,7 @@ const inlineStyles = {
}
}

class CampaignList extends React.Component {
export class CampaignList extends React.Component {
renderRow(campaign) {
const {
isStarted,
Expand All @@ -67,6 +70,7 @@ class CampaignList extends React.Component {
}

const dueByMoment = moment(campaign.dueBy)
const creatorName = campaign.creator ? campaign.creator.displayName : null
const tags = []
if (!isStarted) {
tags.push('Not started')
Expand All @@ -92,6 +96,8 @@ class CampaignList extends React.Component {
Campaign ID: {campaign.id}
<br />
{campaign.description}
{creatorName ?
(<span> &mdash; Created by {creatorName}</span>) : null}
<br />
{dueByMoment.isValid() ?
dueByMoment.format('MMM D, YYYY') :
Expand Down
21 changes: 20 additions & 1 deletion src/migrations/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,26 @@ const migrations = [
})
console.log('added send_before column to message table')
}
}
},
{
auto: true, // 14
date: '2019-02-24',
migrate: async () => {
console.log('adding creator_id field to campaign')
await r.knex.schema.alterTable('campaign', (table) => {
table.integer('creator_id')
.unsigned()
.nullable()
.default(null)
.index()
.references('id')
.inTable('user')
})

console.log('added creator_id field to campaign')
}
},

/* migration template
{auto: true, //if auto is false, then it will block the migration running automatically
date: '2017-08-23',
Expand Down
7 changes: 6 additions & 1 deletion src/server/api/campaign.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,11 @@ export const resolvers = {
return currentEditors(r.redis, campaign, user)
}
return ''
}
},
creator: async (campaign, _, { loaders }) => (
campaign.creator_id
? loaders.user.load(campaign.creator_id)
: null
)
}
}
4 changes: 3 additions & 1 deletion src/server/api/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ const rootMutations = {
} else { // userOrg exists
console.log('existing userOrg ' + userOrg.id + ' user ' + user.id + ' organizationUuid ' + organizationUuid )
}
} else { // no organization
} else { // no organization
console.log('no organization with id ' + organizationUuid + ' for user ' + user.id)
}
return organization
Expand Down Expand Up @@ -519,6 +519,7 @@ const rootMutations = {
await accessRequired(user, campaign.organizationId, 'ADMIN', /* allowSuperadmin=*/ true)
const campaignInstance = new Campaign({
organization_id: campaign.organizationId,
creator_id: user.id,
title: campaign.title,
description: campaign.description,
due_by: campaign.dueBy,
Expand All @@ -534,6 +535,7 @@ const rootMutations = {

const campaignInstance = new Campaign({
organization_id: campaign.organization_id,
creator_id: user.id,
title: 'COPY - ' + campaign.title,
description: campaign.description,
due_by: campaign.dueBy,
Expand Down
2 changes: 2 additions & 0 deletions src/server/models/campaign.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Organization from './organization'
const Campaign = thinky.createModel('campaign', type.object().schema({
id: type.string(),
organization_id: requiredString(),
creator_id: type.string().allowNull(true),
title: optionalString(),
description: optionalString(),
is_started: type
Expand Down Expand Up @@ -55,5 +56,6 @@ const Campaign = thinky.createModel('campaign', type.object().schema({
}).allowExtra(false), { noAutoCreation: true })

Campaign.ensureIndex('organization_id')
Campaign.ensureIndex('creator_id')

export default Campaign

0 comments on commit ad76560

Please sign in to comment.