Skip to content

Commit

Permalink
refact: allow notificationAudience to be pre-set and saved with drafts
Browse files Browse the repository at this point in the history
  • Loading branch information
kewitz committed May 9, 2024
1 parent 47a1613 commit c3bb792
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 2 deletions.
3 changes: 2 additions & 1 deletion server/graphql/common/update.ts
Expand Up @@ -35,6 +35,7 @@ export async function createUpdate(_, args, req) {
CreatedByUserId: req.remoteUser.id,
FromCollectiveId: req.remoteUser.CollectiveId,
makePublicOn: args.update.makePublicOn,
notificationAudience: args.update.notificationAudience,
});

purgeCacheForCollective(collective.slug);
Expand Down Expand Up @@ -76,7 +77,7 @@ export async function publishUpdate(_, args, req) {
checkRemoteUserCanUseUpdates(req);

let update = await fetchUpdateForEdit(args.id, req);
update = await update.publish(req.remoteUser, args.notificationAudience);
update = await update.publish(req.remoteUser, args.notificationAudience || update.notificationAudience);
if (update.isChangelog) {
cache.delete('latest_changelog_publish_date');
}
Expand Down
3 changes: 3 additions & 0 deletions server/graphql/v2/input/UpdateCreateInput.js
@@ -1,6 +1,8 @@
import { GraphQLBoolean, GraphQLInputObjectType, GraphQLNonNull, GraphQLString } from 'graphql';
import { GraphQLDateTime } from 'graphql-scalars';

import { GraphQLUpdateAudienceType } from '../enum';

import { GraphQLAccountReferenceInput } from './AccountReferenceInput';

export const GraphQLUpdateCreateInput = new GraphQLInputObjectType({
Expand All @@ -14,5 +16,6 @@ export const GraphQLUpdateCreateInput = new GraphQLInputObjectType({
html: { type: new GraphQLNonNull(GraphQLString) },
fromAccount: { type: GraphQLAccountReferenceInput },
account: { type: new GraphQLNonNull(GraphQLAccountReferenceInput) },
notificationAudience: { type: GraphQLUpdateAudienceType },
}),
});
3 changes: 3 additions & 0 deletions server/graphql/v2/input/UpdateUpdateInput.js
@@ -1,6 +1,8 @@
import { GraphQLBoolean, GraphQLInputObjectType, GraphQLNonNull, GraphQLString } from 'graphql';
import { GraphQLDateTime } from 'graphql-scalars';

import { GraphQLUpdateAudienceType } from '../enum';

import { GraphQLAccountReferenceInput } from './AccountReferenceInput';

export const GraphQLUpdateUpdateInput = new GraphQLInputObjectType({
Expand All @@ -15,5 +17,6 @@ export const GraphQLUpdateUpdateInput = new GraphQLInputObjectType({
makePublicOn: { type: GraphQLDateTime },
html: { type: GraphQLString },
fromAccount: { type: GraphQLAccountReferenceInput },
notificationAudience: { type: GraphQLUpdateAudienceType },
}),
});
19 changes: 18 additions & 1 deletion server/models/Update.ts
Expand Up @@ -108,7 +108,24 @@ class Update extends Model<InferAttributes<Update>, InferCreationAttributes<Upda
}
}

const editableAttributes = ['TierId', 'title', 'html', 'tags', 'isPrivate', 'isChangelog', 'makePublicOn'];
if (
this.publishedAt &&
newUpdateData.notificationAudience &&
newUpdateData.notificationAudience !== this.notificationAudience
) {
throw new errors.ValidationFailed('Cannot change the notification audience of a published update');
}

const editableAttributes = [
'TierId',
'title',
'html',
'tags',
'isPrivate',
'isChangelog',
'makePublicOn',
'notificationAudience',
];

return await this.update({
...pick(newUpdateData, editableAttributes),
Expand Down
22 changes: 22 additions & 0 deletions test/server/graphql/v2/mutation/UpdateMutations.test.js
Expand Up @@ -329,6 +329,27 @@ describe('server/graphql/v2/mutation/UpdateMutations', () => {
expect(result.data.editUpdate.slug).to.equal('new-title');
});

it('fails if user tries to update notificationAudience of a published update', async () => {
await models.Update.update(
{ slug: 'first-update-and-love', publishedAt: new Date(), notificationAudience: 'FINANCIAL_CONTRIBUTORS' },
{ where: { id: update1.id } },
);
const result = await utils.graphqlQueryV2(
editUpdateMutation,
{
update: {
id: idEncode(update1.id, IDENTIFIER_TYPES.UPDATE),
title: 'new title',
notificationAudience: 'NO_ONE',
},
},
user1,
);

expect(result.errors).to.exist;
expect(result.errors[0].message).to.equal('Cannot change the notification audience of a published update');
});

it("edits an update successfully and doesn't change the slug if published", async () => {
await models.Update.update(
{ slug: 'first-update-and-love', publishedAt: new Date() },
Expand All @@ -339,6 +360,7 @@ describe('server/graphql/v2/mutation/UpdateMutations', () => {
{ update: { id: idEncode(update1.id, IDENTIFIER_TYPES.UPDATE), title: 'new title' } },
user1,
);
result.errors && console.error(result.errors[0]);
expect(result.errors).to.not.exist;
expect(result.data.editUpdate.slug).to.equal('first-update-and-love');
await models.Update.update({ publishedAt: null }, { where: { id: update1.id } });
Expand Down

0 comments on commit c3bb792

Please sign in to comment.