Skip to content

Commit

Permalink
UpdateProgram in Gateway (#273)
Browse files Browse the repository at this point in the history
* ✨ Update Program on gateway

* ✨ Grab current program state as default to update

* 💡 Added comment to explain missing input fields
  • Loading branch information
joneubank authored Jul 16, 2019
1 parent ef61041 commit dbecd5a
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 4 deletions.
44 changes: 41 additions & 3 deletions server/schemas/Program/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { gql } from 'apollo-server-express';
import { makeExecutableSchema } from 'graphql-tools';
import { get } from 'lodash';
import { get, pickBy } from 'lodash';

import programService from '../../services/programService';
import { wrapValue } from '../../utils/grpcUtils';
Expand Down Expand Up @@ -75,6 +75,21 @@ const typeDefs = gql`
admins: [ProgramUserInput!]!
}
input UpdateProgramInput {
# This intentionally does not provide access to submittedDonors or genomicDonors
# Those are maintained by an internal service and should not be updated by any client through the gateway
name: String
description: String
commitmentDonors: Int
website: String
institutions: String
countries: String
regions: String
membershipType: MembershipType
cancerTypes: [String]
primarySites: [String]
}
input InviteUserInput {
programShortName: String!
userFirstName: String!
Expand Down Expand Up @@ -107,10 +122,17 @@ const typeDefs = gql`
type Mutation {
"""
Create new program
Returns the shortName of the program if successfully created
For lists (Cancer Type, Primary Site, Institution, Regions, Countries) the entire new value must be provided, not just values being added.
Returns Program object details of created program
"""
createProgram(program: ProgramInput!): Program @cost(complexity: 10)
"""
Update Program
Returns shortName of the program if succesfully updated
"""
updateProgram(shortName: String!, updates: UpdateProgramInput!): String @cost(complexity: 10)
"""
Invite a user to join a program
Returns the email of the user if the invite is successfully sent
Expand Down Expand Up @@ -167,7 +189,7 @@ const resolvers = {
const { egoToken } = context;
const response = await programService.getProgram(args.shortName, egoToken);
const programDetails = get(response, 'program');
return response === null ? null : convertGrpcProgramToGql(programDetails);
return response ? convertGrpcProgramToGql(programDetails) : null;
},
programs: async (obj, args, context, info) => {
const { egoToken } = context;
Expand All @@ -188,6 +210,22 @@ const resolvers = {
const programDetails = get(programResponse, 'program');
return programResponse === null ? null : convertGrpcProgramToGql(programDetails);
},
updateProgram: async (obj, args, context, info) => {
const { egoToken } = context;
const updates = pickBy(get(args, 'updates', {}), v => v !== undefined);
const shortName = get(args, 'shortName', {});

// // Update program takes the complete program object future state
const currentPorgramResponse = await programService.getProgram(shortName, egoToken);
const currentProgramDetails = convertGrpcProgramToGql(
get(currentPorgramResponse, 'program', {}),
);

const combinedUpdates = { ...currentProgramDetails, ...updates };

const response = await programService.updateProgram(shortName, combinedUpdates, egoToken);
return response === null ? null : get(args, 'shortName');
},
inviteUser: async (obj, args, context, info) => {
const { egoToken } = context;
const invite = get(args, 'invite', {});
Expand Down
57 changes: 56 additions & 1 deletion server/services/programService/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,53 @@ const createProgram = async (
});
};

const updateProgram = async (
shortName,
{
name,
description,
commitmentDonors,
submittedDonors,
genomicDonors,
website,
institutions,
countries,
regions,
membershipType,
cancerTypes,
primarySites,
},
jwt = null,
) => {
const updateProgramRequest = {
program: {
short_name: wrapValue(shortName),
name: wrapValue(name),
description: wrapValue(description),
commitment_donors: wrapValue(commitmentDonors),
website: wrapValue(website),
institutions: wrapValue(institutions),
countries: wrapValue(countries),
regions: wrapValue(regions),
submitted_donors: wrapValue(submittedDonors),
genomic_donors: wrapValue(genomicDonors),

membership_type: wrapValue(membershipType),

cancer_types: cancerTypes,
primary_sites: primarySites,
},
};

return await new Promise((resolve, reject) => {
programService.updateProgram(
updateProgramRequest,
getAuthMeta(jwt),
defaultPromiseCallback(resolve, reject),
);
});
};

const inviteUser = async (
{ programShortName, userFirstName, userLastName, userEmail, userRole },
jwt = null,
Expand Down Expand Up @@ -157,4 +204,12 @@ const joinProgram = async (
};

// const inviteUser = async ({programShortName, }, jwt=null)
export default { getProgram, listPrograms, listUsers, createProgram, inviteUser, joinProgram };
export default {
getProgram,
listPrograms,
listUsers,
createProgram,
updateProgram,
inviteUser,
joinProgram,
};

0 comments on commit dbecd5a

Please sign in to comment.