File size: 2,149 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
// @flow
import type { GraphQLContext } from '../../';
import UserError from '../../utils/UserError';
import { removeMembersInCommunity } from '../../models/usersCommunities';
import { deleteCommunity } from '../../models/community';
import { getChannelsByCommunity, deleteChannel } from '../../models/channel';
import { getThreadsByCommunity } from '../../models/thread';
import { removeMembersInChannel } from '../../models/usersChannels';
import { deleteThread } from '../../models/thread';
import {
isAuthedResolver as requireAuth,
canAdministerCommunity,
} from '../../utils/permissions';
type Input = {
communityId: string,
};
export default requireAuth(async (_: any, args: Input, ctx: GraphQLContext) => {
const { communityId } = args;
const { user, loaders } = ctx;
const communityToEvaluate = await loaders.community.load(communityId);
if (!communityToEvaluate || communityToEvaluate.deletedAt) {
return new UserError("This community doesn't exist.");
}
if (!(await canAdministerCommunity(user.id, communityId, loaders))) {
return new UserError(
"You don't have permission to make changes to this community."
);
}
const [allChannelsInCommunity, allThreadsInCommunity] = await Promise.all([
getChannelsByCommunity(communityId),
getThreadsByCommunity(communityId),
removeMembersInCommunity(communityId),
deleteCommunity(communityId, user.id),
]);
// after a community has been deleted, we need to mark all the channels
// as deleted
const removeAllChannels = allChannelsInCommunity.map(channel =>
deleteChannel(channel.id, user.id)
);
// and remove all relationships to the deleted channels
const removeAllRelationshipsToChannels = allChannelsInCommunity.map(channel =>
removeMembersInChannel(channel.id)
);
// and mark all the threads in that community as deleted
const removeAllThreadsInCommunity = allThreadsInCommunity.map(thread =>
deleteThread(thread.id, user.id)
);
return Promise.all([
...removeAllChannels,
...removeAllRelationshipsToChannels,
...removeAllThreadsInCommunity,
]).then(() => communityToEvaluate);
});
|