-
Notifications
You must be signed in to change notification settings - Fork 2
feat(campaign-builder): allow unstarting campaign #250
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| import Button from "@material-ui/core/Button"; | ||
| import { useSetCampaignArchivedMutation } from "@spoke/spoke-codegen"; | ||
| import React from "react"; | ||
|
|
||
| export interface ArchiveCampaignButtonProps { | ||
| campaignId: string; | ||
| isArchived: boolean; | ||
| } | ||
|
|
||
| export const ArchiveCampaignButton: React.FC<ArchiveCampaignButtonProps> = ( | ||
| props | ||
| ) => { | ||
| const { campaignId, isArchived } = props; | ||
| const [setCampaignArchived] = useSetCampaignArchivedMutation(); | ||
|
|
||
| return ( | ||
| <Button | ||
| variant="outlined" | ||
| onClick={() => | ||
| setCampaignArchived({ | ||
| variables: { campaignId, archived: !isArchived } | ||
| }) | ||
| } | ||
| > | ||
| {isArchived ? "Unarchive" : "Archive"} | ||
| </Button> | ||
| ); | ||
| }; | ||
|
|
||
| export default ArchiveCampaignButton; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import Button from "@material-ui/core/Button"; | ||
| import { useUnstartCampaignMutation } from "@spoke/spoke-codegen"; | ||
| import React from "react"; | ||
|
|
||
| export interface UnstartCampaignButtonProps { | ||
| campaignId: string; | ||
| onError: (errorMessage: string) => void; | ||
| } | ||
|
|
||
| export const UnstartCampaignButton: React.FC<UnstartCampaignButtonProps> = ( | ||
| props | ||
| ) => { | ||
| const { campaignId, onError } = props; | ||
| const [ | ||
| unstartCampaign, | ||
| { loading: unstarting } | ||
| ] = useUnstartCampaignMutation(); | ||
|
|
||
| const handleClick = async () => { | ||
| const result = await unstartCampaign({ variables: { campaignId } }); | ||
| if (result.errors) { | ||
| onError(result.errors.map((error) => error.message).join(", ")); | ||
| } | ||
| }; | ||
|
|
||
| return ( | ||
| <Button variant="outlined" disabled={unstarting} onClick={handleClick}> | ||
| Unstart | ||
| </Button> | ||
| ); | ||
| }; | ||
|
|
||
| export default UnstartCampaignButton; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -59,6 +59,7 @@ import { getStepsToUpdate } from "./lib/bulk-script-editor"; | |
| import { | ||
| copyCampaign, | ||
| editCampaign, | ||
| hasSentMessages, | ||
| markAutosendingPaused, | ||
| unqueueAutosending | ||
| } from "./lib/campaign"; | ||
|
|
@@ -1016,6 +1017,30 @@ const rootMutations = { | |
| return campaign; | ||
| }, | ||
|
|
||
| unstartCampaign: async (_root, { id }, { user, loaders }) => { | ||
| const { organization_id } = await loaders.campaign.load(id); | ||
| await accessRequired(user, organization_id, "ADMIN", true); | ||
|
|
||
| if (await hasSentMessages(id)) { | ||
| throw new ForbiddenError( | ||
| "Campaign cannot be unstarted after messages have been sent." | ||
| ); | ||
| } | ||
|
|
||
| const [campaign] = await r | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we we also stop any queued autosend jobs here? Thinking about the case where messages already waiting in the queue could still be sent after the campaign is unstarted, even though no message records exist yet
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great catch! I think rather than deleting the jobs, we should just check |
||
| .knex("campaign") | ||
| .update({ is_started: false }) | ||
| .where({ id }) | ||
| .returning("*"); | ||
|
|
||
| const memoizer = await MemoizeHelper.getMemoizer(); | ||
| await memoizer.invalidate(cacheOpts.CampaignsList.key, { | ||
| organizationId: organization_id | ||
| }); | ||
|
|
||
| return campaign; | ||
| }, | ||
|
|
||
| editCampaign: async ( | ||
| _root, | ||
| { id, campaign: campaignEdits }, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Curious about the use of
r.reader.rawhere instead ofr.knex.raw. This might be intentional, but wouldn’t it make sense to check the primary db so we don’t miss a recently created message?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep totally makes sense. tbh i think the way it's been used in the codebase for the most part has been very simplistic "reads -> reader, writes -> knex" so i'll fix this and make a followup issue for more comprehensive review of other places this could happen which is definitely needed 😅