import { GraphQLBoolean, GraphQLNonNull, GraphQLString, } from 'graphql'; import { IProject, ICreateProject, ISelectProject, IUpdateProject, } from '../../model/renovationProject'; import { createRenovationProject, deleteRenovationProject, updateRenovationProject, exportRenovationProject, } from '../renovationProjectRepository'; import { authorized } from '../../auth'; import { publish } from '../../pubsub'; import { PROJECT_CREATED, PROJECT_UPDATED, PROJECT_DELETED, } from './events'; import { projectType, createProjectInputType, updateProjectInputType, selectProjectInputType, } from './types'; interface ICreateProjectInput { input: ICreateProject; } interface IUpdateProjectInput { input: IUpdateProject; } interface ISelectProjectInput { input: ISelectProject; } export default { createRenovationProject: { description: 'Creates a new renovation project', type: projectType, args: { input: { type: new GraphQLNonNull(createProjectInputType) }, }, resolve: authorized( ['AM', 'CE', 'CD'], async (_: any, { input }: ICreateProjectInput) => { const project: Partial = await createRenovationProject(input); publish(PROJECT_CREATED, project); return project; }, ), }, updateRenovationProject: { description: 'Updates renovation project', type: projectType, args: { input: { type: new GraphQLNonNull(updateProjectInputType) }, }, resolve: authorized( ['AM', 'CE', 'CD'], async (_: any, { input }: IUpdateProjectInput) => { const project: Partial = await updateRenovationProject(input); publish(PROJECT_UPDATED, project); return project; }, ), }, exportRenovationProject: { description: 'Exports renovation project to Excel', type: GraphQLString, args: { input: { type: new GraphQLNonNull(selectProjectInputType) }, }, resolve: authorized( ['AM', 'CE', 'CD'], async (_: any, { input }: ISelectProjectInput) => { return await exportRenovationProject(input); }, ), }, deleteRenovationProject: { description: 'Delete renovation project', type: GraphQLBoolean, args: { input: { type: new GraphQLNonNull(selectProjectInputType) }, }, resolve: authorized( ['AM', 'CE', 'CD'], async (_: any, { input }: ISelectProjectInput) => { const deleted = await deleteRenovationProject(input); if (deleted) { publish(PROJECT_DELETED, { id: input.id, }); } return deleted; }, ), }, };