Skip to content

Commit

Permalink
Merge branch 'master' into subs_cluster_id
Browse files Browse the repository at this point in the history
  • Loading branch information
rmgraham authored Feb 9, 2021
2 parents e8ef81e + a731790 commit 9a16809
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 6 deletions.
5 changes: 5 additions & 0 deletions app/apollo/models/channel.schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ const ChannelSchema = new mongoose.Schema({
}
}
],
tags: [
{
type: String,
}
],
}, {
strict:'throw',
});
Expand Down
30 changes: 26 additions & 4 deletions app/apollo/resolvers/channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const { applyQueryFieldsToChannels } = require('../utils/applyQueryFields');
const yaml = require('js-yaml');

const { ACTIONS, TYPES, CHANNEL_VERSION_YAML_MAX_SIZE_LIMIT_MB, CHANNEL_LIMITS, CHANNEL_VERSION_LIMITS } = require('../models/const');
const { whoIs, validAuth, getAllowedChannels, NotFoundError, RazeeValidationError, BasicRazeeError, RazeeQueryError} = require ('./common');
const { whoIs, validAuth, getAllowedChannels, filterChannelsToAllowed, NotFoundError, RazeeValidationError, BasicRazeeError, RazeeQueryError} = require ('./common');

const { encryptOrgData, decryptOrgData} = require('../../utils/orgs');

Expand Down Expand Up @@ -87,6 +87,26 @@ const channelResolvers = {
}
return channel;
},
channelsByTags: async(parent, { orgId, tags }, context, fullQuery)=>{
const queryFields = GraphqlFields(fullQuery);
const { models, me, req_id, logger } = context;
const queryName = 'channelsByTags';
logger.debug({req_id, user: whoIs(me), orgId, tags}, `${queryName} enter`);

try{
if(tags.length < 1){
throw new RazeeValidationError('Please supply one or more tags', context);
}
var channels = await models.Channel.find({ org_id: orgId, tags: { $all: tags } });
channels = await filterChannelsToAllowed(me, orgId, ACTIONS.READ, TYPES.CHANNEL, channels, context);
await applyQueryFieldsToChannels(channels, queryFields, { orgId }, context);

}catch(err){
logger.error(err, `${queryName} encountered an error when serving ${req_id}.`);
throw new RazeeQueryError(`Query ${queryName} error. ${err.message}`, context);
}
return channels;
},
channelVersionByName: async(parent, { orgId: org_id, channelName, versionName }, context) => {
const { me, req_id, logger } = context;
const queryName = 'channelVersionByName';
Expand Down Expand Up @@ -153,7 +173,7 @@ const channelResolvers = {
}
},
Mutation: {
addChannel: async (parent, { orgId: org_id, name }, context)=>{
addChannel: async (parent, { orgId: org_id, name, tags=[] }, context)=>{
const { models, me, req_id, logger } = context;
const queryName = 'addChannel';
logger.debug({ req_id, user: whoIs(me), org_id, name }, `${queryName} enter`);
Expand All @@ -175,6 +195,7 @@ const channelResolvers = {
await models.Channel.create({
_id: UUID(),
uuid, org_id, name, versions: [],
tags,
});
return {
uuid,
Expand All @@ -187,7 +208,7 @@ const channelResolvers = {
throw new RazeeQueryError(`Query ${queryName} error. ${err.message}`, context);
}
},
editChannel: async (parent, { orgId: org_id, uuid, name }, context)=>{
editChannel: async (parent, { orgId: org_id, uuid, name, tags=[] }, context)=>{
const { models, me, req_id, logger } = context;
const queryName = 'editChannel';
logger.debug({ req_id, user: whoIs(me), org_id, uuid, name }, `${queryName} enter`);
Expand All @@ -198,7 +219,7 @@ const channelResolvers = {
throw new NotFoundError(`channel uuid "${uuid}" not found`, context);
}
await validAuth(me, org_id, ACTIONS.UPDATE, TYPES.CHANNEL, queryName, context, [channel.uuid, channel.name]);
await models.Channel.updateOne({ org_id, uuid }, { $set: { name } });
await models.Channel.updateOne({ org_id, uuid }, { $set: { name, tags } });

// find any subscriptions for this channel and update channelName in those subs
await models.Subscription.updateMany(
Expand All @@ -210,6 +231,7 @@ const channelResolvers = {
uuid,
success: true,
name,
tags,
};
} catch(err){
if (err instanceof BasicRazeeError) {
Expand Down
11 changes: 9 additions & 2 deletions app/apollo/schema/channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@ const channelSchema = gql`
created: Date!
versions: [ChannelVersion]
subscriptions: [ChannelSubscription]
tags: [String!]!
}
type AddChannelReply {
uuid: String!
}
type EditChannelReply {
uuid: String!
name: String!
tags: [String!]!
success: Boolean
}
type AddChannelVersionReply {
Expand Down Expand Up @@ -81,6 +83,11 @@ const channelSchema = gql`
"""
channelByName(orgId: String! @sv, name: String! @sv): Channel
"""
Gets channels that contain all passed tags
"""
channelsByTags(orgId: String! @sv, tags: [String!]!): [Channel]!
"""
Gets a channel version info from this channel uuid and version uuid
"""
Expand All @@ -96,12 +103,12 @@ const channelSchema = gql`
"""
Adds a channel
"""
addChannel(orgId: String! @sv, name: String! @sv): AddChannelReply!
addChannel(orgId: String! @sv, name: String! @sv, tags: [String!]): AddChannelReply!
"""
Edits a channel
"""
editChannel(orgId: String! @sv, uuid: String! @sv, name: String! @sv): EditChannelReply!
editChannel(orgId: String! @sv, uuid: String! @sv, name: String! @sv, tags: [String!]): EditChannelReply!
"""
Adds a yaml version to this channel
Expand Down
6 changes: 6 additions & 0 deletions app/apollo/utils/applyQueryFields.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,12 @@ const applyQueryFieldsToChannels = async(channels, queryFields={}, args, context
const { models, me } = context;
var { orgId } = args;

_.each(channels, (channel)=>{
if(!channel.tags){
channel.tags = [];
}
});

if(queryFields.subscriptions){
//piggyback basic-info of subscriptions associated with this channel that user allowed to see
const conditions = await getGroupConditions(me, orgId, ACTIONS.READ, 'name', 'applyQueryFieldsToChannels queryFields.subscriptions', context);
Expand Down

0 comments on commit 9a16809

Please sign in to comment.