Skip to content

Commit

Permalink
fix couple issues from merge
Browse files Browse the repository at this point in the history
  • Loading branch information
matheus-relief committed Feb 14, 2024
1 parent a6a8544 commit 17ca1fd
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 95 deletions.
1 change: 1 addition & 0 deletions src/schema/mutation/addUsers.mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { graphQLAuthCheck } from '@schema/shared';
import { UserArgs, UserInputType } from '@schema/inputs/user.input';
import { Types } from 'mongoose';
import { Context } from '@server/apollo/context';
import pubsub from '../../server/pubsub';

/** Arguments for the addUsers mutation */
type AddUsersArgs = {
Expand Down
6 changes: 3 additions & 3 deletions src/schema/mutation/deletePullJob.mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ export default {
.where({ _id: args.id })
.getFilter();
const pullJob = await PullJob.findOneAndDelete(filters);
if (!pullJob)
if (!pullJob.ok)
throw new GraphQLError(
context.i18next.t('common.errors.permissionNotGranted')
);

unscheduleJob(pullJob);
return pullJob;
unscheduleJob(pullJob.value);
return pullJob.value;
} catch (err) {
logger.error(err.message, { stack: err.stack });
if (err instanceof GraphQLError) {
Expand Down
109 changes: 17 additions & 92 deletions src/schema/mutation/editPullJob.mutation.ts
Original file line number Diff line number Diff line change
@@ -1,120 +1,45 @@
import {
GraphQLError,
GraphQLID,
GraphQLList,
GraphQLNonNull,
GraphQLString,
} from 'graphql';
import { GraphQLError, GraphQLID, GraphQLNonNull } from 'graphql';
import { PullJobType } from '../types';
import { StatusType, status } from '@const/enumTypes';
import { Channel, Form, PullJob } from '@models';
import { PullJob } from '@models';
import { AppAbility } from '@security/defineUserAbility';
import { StatusEnumType } from '@const/enumTypes';
import GraphQLJSON from 'graphql-type-json';
import { scheduleJob, unscheduleJob } from '../../server/pullJobScheduler';
import { unscheduleJob } from '../../server/pullJobScheduler';
import { logger } from '@services/logger.service';
import { accessibleBy } from '@casl/mongoose';
import { graphQLAuthCheck } from '@schema/shared';
import { Types } from 'mongoose';
import { Context } from '@server/apollo/context';

/** Arguments for the editPullJob mutation */
type EditPullJobArgs = {
/** Arguments for the deletePullJob mutation */
type DeletePullJobArgs = {
id: string | Types.ObjectId;
name?: string;
status?: StatusType;
apiConfiguration?: string | Types.ObjectId;
url?: string;
path?: string;
schedule?: string;
convertTo?: string | Types.ObjectId;
mapping?: any;
uniqueIdentifiers?: string[];
channel?: string | Types.ObjectId;
};

/**
* Edit an existing pullJob if authorized.
* Delete a pullJob
*/
export default {
type: PullJobType,
args: {
id: { type: new GraphQLNonNull(GraphQLID) },
name: { type: GraphQLString },
status: { type: StatusEnumType },
apiConfiguration: { type: GraphQLID },
url: { type: GraphQLString },
path: { type: GraphQLString },
schedule: { type: GraphQLString },
convertTo: { type: GraphQLID },
mapping: { type: GraphQLJSON },
uniqueIdentifiers: { type: new GraphQLList(GraphQLString) },
channel: { type: GraphQLID },
},
async resolve(parent, args: EditPullJobArgs, context: Context) {
async resolve(parent, args: DeletePullJobArgs, context: Context) {
graphQLAuthCheck(context);
try {
const user = context.user;
const ability: AppAbility = user.ability;

if (args.convertTo) {
const form = await Form.findById(args.convertTo);
if (!form)
throw new GraphQLError(
context.i18next.t('common.errors.dataNotFound')
);
}

if (args.channel) {
const filters = {
_id: args.channel,
};
const channel = await Channel.findOne(filters);
if (!channel)
throw new GraphQLError(
context.i18next.t('common.errors.dataNotFound')
);
}

const update = {};
Object.assign(
update,
args.name && { name: args.name },
args.status && { status: args.status },
args.apiConfiguration && { apiConfiguration: args.apiConfiguration },
args.url && { url: args.url },
args.path && { path: args.path },
args.schedule && { schedule: args.schedule },
args.convertTo && { convertTo: args.convertTo },
args.mapping && { mapping: args.mapping },
args.uniqueIdentifiers && { uniqueIdentifiers: args.uniqueIdentifiers },
args.channel && { channel: args.channel }
);
const filters = PullJob.find(accessibleBy(ability, 'update').PullJob)
const filters = PullJob.find(accessibleBy(ability, 'delete').PullJob)
.where({ _id: args.id })
.getFilter();
try {
const pullJob = await PullJob.findOneAndUpdate(filters, update, {
new: true,
runValidators: true,
}).populate({
path: 'apiConfiguration',
model: 'ApiConfiguration',
});
if (!pullJob)
throw new GraphQLError(
context.i18next.t('common.errors.dataNotFound')
);
if (pullJob.status === status.active) {
scheduleJob(pullJob);
} else {
unscheduleJob(pullJob);
}
return pullJob;
} catch (err) {
logger.error(err.message);
throw new GraphQLError(err.message);
}

const pullJob = await PullJob.findOneAndDelete(filters);
if (!pullJob.ok)
throw new GraphQLError(
context.i18next.t('common.errors.permissionNotGranted')
);

unscheduleJob(pullJob.value);
return pullJob.value;
} catch (err) {
logger.error(err.message, { stack: err.stack });
if (err instanceof GraphQLError) {
Expand Down
2 changes: 2 additions & 0 deletions src/schema/mutation/editUser.mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { logger } from '@services/logger.service';
import { graphQLAuthCheck } from '@schema/shared';
import { Types } from 'mongoose';
import { Context } from '@server/apollo/context';
import pubsub from '../../server/pubsub';

/** Arguments for the editUser mutation */
type EditUserArgs = {
Expand Down Expand Up @@ -163,6 +164,7 @@ export default {
await Notification.insertMany(
notifications.map((x) => x.notification)
);

const publisher = await pubsub();
notifications.forEach((x) => {
publisher.publish(x.channel.id, { notification: x.notification });
Expand Down

0 comments on commit 17ca1fd

Please sign in to comment.