Skip to content

Commit

Permalink
Merge branch 'master' into translation
Browse files Browse the repository at this point in the history
  • Loading branch information
tasneem067 committed Feb 11, 2021
2 parents a8aae72 + e09332f commit 5563b25
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 15 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

Razeedash-API is the interface used by

- app.razee.io
- [Razeedash](https://github.com/razee-io/Razeedash)
- [watch-keeper](https://github.com/razee-io/watch-keeper)

## Requirements
Expand Down
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
3 changes: 3 additions & 0 deletions app/apollo/models/subscription.schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ const SubscriptionSchema = new mongoose.Schema({
type: String,
}
],
clusterId: {
type: String,
},
channel_uuid: {
type: String,
alias: 'channelUuid',
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(context.req.t('Query {{queryName}} error. {{err.message}}', {'queryName':queryName, 'err.message':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(context.req.t('channel uuid "{{uuid}}" not found', {'uuid':uuid}), 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
29 changes: 23 additions & 6 deletions app/apollo/resolvers/subscription.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@ const subscriptionResolvers = {
// subscription groups: ['dev', 'prod'] , clusterGroupNames: ['stage'] ==> false
var foundSubscriptions = await models.Subscription.find({
'org_id': org_id,
groups: { $in: clusterGroupNames },
$or: [
{ groups: { $in: clusterGroupNames } },
{ clusterId: cluster_id },
],
}).lean(/* skip virtuals: true for now since it is class facing api. */);
_.each(foundSubscriptions, (sub)=>{
if(_.isUndefined(sub.channelName)){
Expand Down Expand Up @@ -202,7 +205,13 @@ const subscriptionResolvers = {
// subscription groups: ['dev', 'prod'] , clusterGroupNames: ['dev', 'prod'] ==> true
// subscription groups: ['dev', 'prod'] , clusterGroupNames: ['dev', 'prod', 'stage'] ==> true
// subscription groups: ['dev', 'prod'] , clusterGroupNames: ['stage'] ==> false
var subscriptions = await models.Subscription.find({org_id, groups: { $in: clusterGroupNames },}).lean({ virtuals: true });
var subscriptions = await models.Subscription.find({
org_id,
$or: [
{ groups: { $in: clusterGroupNames } },
{ clusterId: cluster_id },
],
}).lean({ virtuals: true });
subscriptions = await filterSubscriptionsToAllowed(me, org_id, ACTIONS.READ, TYPES.SUBSCRIPTION, subscriptions, context);
}catch(err){
logger.error(err);
Expand Down Expand Up @@ -256,7 +265,13 @@ const subscriptionResolvers = {
// subscription groups: ['dev', 'prod'] , clusterGroupNames: ['dev', 'prod'] ==> true
// subscription groups: ['dev', 'prod'] , clusterGroupNames: ['dev', 'prod', 'stage'] ==> true
// subscription groups: ['dev', 'prod'] , clusterGroupNames: ['stage'] ==> false
var subscriptions = await models.Subscription.find({org_id, groups: { $in: clusterGroupNames },}).lean({ virtuals: true });
var subscriptions = await models.Subscription.find({
org_id,
$or: [
{ groups: { $in: clusterGroupNames } },
{ clusterId: cluster.cluster_id },
]
}).lean({ virtuals: true });
subscriptions = await filterSubscriptionsToAllowed(me, org_id, ACTIONS.READ, TYPES.SUBSCRIPTION, subscriptions, context);
}catch(err){
logger.error(err);
Expand All @@ -277,7 +292,7 @@ const subscriptionResolvers = {
}
},
Mutation: {
addSubscription: async (parent, { orgId: org_id, name, groups, channelUuid: channel_uuid, versionUuid: version_uuid }, context)=>{
addSubscription: async (parent, { orgId: org_id, name, groups=[], channelUuid: channel_uuid, versionUuid: version_uuid, clusterId=null }, context)=>{
const { models, me, req_id, logger } = context;
const queryName = 'addSubscription';
logger.debug({req_id, user: whoIs(me), org_id }, `${queryName} enter`);
Expand Down Expand Up @@ -312,7 +327,8 @@ const subscriptionResolvers = {
await models.Subscription.create({
_id: UUID(),
uuid, org_id, name, groups, owner: me._id,
channelName: channel.name, channel_uuid, version: version.name, version_uuid
channelName: channel.name, channel_uuid, version: version.name, version_uuid,
clusterId,
});

pubSub.channelSubChangedFunc({org_id: org_id}, context);
Expand All @@ -329,7 +345,7 @@ const subscriptionResolvers = {
throw new RazeeQueryError(context.req.t('Query {{queryName}} error. {{err.message}}', {'queryName':queryName, 'err.message':err.message}), context);
}
},
editSubscription: async (parent, { orgId, uuid, name, groups, channelUuid: channel_uuid, versionUuid: version_uuid }, context)=>{
editSubscription: async (parent, { orgId, uuid, name, groups=[], channelUuid: channel_uuid, versionUuid: version_uuid, clusterId=null }, context)=>{
const { models, me, req_id, logger } = context;
const queryName = 'editSubscription';
logger.debug({req_id, user: whoIs(me), orgId }, `${queryName} enter`);
Expand Down Expand Up @@ -363,6 +379,7 @@ const subscriptionResolvers = {
var sets = {
name, groups,
channelName: channel.name, channel_uuid, version: version.name, version_uuid,
clusterId,
};
await models.Subscription.updateOne({ uuid, org_id: orgId, }, { $set: sets });

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
5 changes: 3 additions & 2 deletions app/apollo/schema/subscription.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const subscriptionSchema = gql`
orgId: String!
name: String!
groups: [String!]
clusterId: String
channelUuid: String!
channelName: String!
channel: Channel
Expand Down Expand Up @@ -112,12 +113,12 @@ const subscriptionSchema = gql`
"""
Adds a subscription
"""
addSubscription(orgId: String! @sv, name: String! @sv, groups: [String!]! @sv, channelUuid: String! @sv, versionUuid: String! @sv): AddChannelSubscriptionReply!
addSubscription(orgId: String! @sv, name: String! @sv, groups: [String!] @sv, channelUuid: String! @sv, versionUuid: String! @sv, clusterId: String @sv): AddChannelSubscriptionReply!
"""
Edits a subscription
"""
editSubscription(orgId: String! @sv, uuid: String! @sv, name: String! @sv, groups: [String!]! @sv, channelUuid: String! @sv, versionUuid: String! @sv): EditChannelSubscriptionReply!
editSubscription(orgId: String! @sv, uuid: String! @sv, name: String! @sv, groups: [String!]! @sv, channelUuid: String! @sv, versionUuid: String! @sv, clusterId: String @sv): EditChannelSubscriptionReply!
"""
Set a configurationVersion
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 5563b25

Please sign in to comment.