Skip to content
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

fix: re-fetch campaign details after campaign is sent #1116

Merged
merged 9 commits into from
Apr 14, 2021

Conversation

zwliew
Copy link
Contributor

@zwliew zwliew commented Apr 6, 2021

Problem

When a user first sends a campaign, sentAt remains undefined. This causes the file name of downloaded delivery reports to contain invalid_Date. This issue doesn't occur after refreshing the page since the refresh causes sentAt to be refetched from the backend.

Closes #1113

Solution

Improvements:

  • Refactor the onModalConfirm handler to a common utilility function.

Bug Fixes:

  • Set the sentAt field to the current Date when sending a campaign. This shouldn't be far off from the actual value in the backend, since the actual value is set when creating sending jobs as part of the API request handler for sending a campaign.
  • Correct the type definitions for sentAt and statusUpdatedAt. These are should be of type string | undefined.

Before & After Screenshots

There are no visual changes.

Tests

  1. Deploy the change on Amplify.
  2. Send a campaign and wait for the delivery report to be generated without refreshing the page.
  3. Download the delivery report. The filename should represent the same date as the one shown in the "Sent Date" column.
  4. Repeat the test for every campaign type (SMS, Telegram, Email, Protected Email).

Deploy Notes

There are no deploy notes.

zwliew added 4 commits April 6, 2021 11:23
Even though sent_at, created_at, and status_updated_at are represented
as Dates in the backend, these fields will be serialized to an ISO
string which the frontend then receives.

Hence, correct the type definition of these fields.
When sending a campaign for the first time, the `sentAt` field for the
in-memory representation of `Campaign` is still undefined. This causes
"invalid_Date" to appear in the filename of delivery reports.

Fix this by updating the `sentAt` field of the in-memory `Campaign` to
the current date when sending the campaign for the first time.

Fixes issue #1113
if (sendRate) {
sendUserEvent(GA_USER_EVENTS.USE_SEND_RATE, channelType)
}
updateCampaign({ status: Status.Sending, sentAt: new Date().toISOString() })
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if we should instead fetch the sent_at value from the backend instead. In practice, I don't think the disparity between the 2 values will be significant, since the sent_at value is set immediately in the backend anyway.

Copy link
Contributor

@lamkeewei lamkeewei Apr 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gave some thought to this, would prefer to refetch the campaign details and update campaign in CampaignContext. Would prefer not to sacrifice de-sync between local and backend state to save one network call. Was thinking we can make call to backend to refetch campaign once status changed to Status.Sent.

Maybe something like that?

useEffect(() => {
  let timeoutId: NodeJS.Timeout

  async function poll() {
    const { status } = await refreshCampaignStats(id)

    if (status !== Status.Sent) {
      timeoutId = setTimeout(poll, 2000)
    } else if (status === Status.Sent) {
      const updated = await getCampaignDetails(campaign.id)
      setCampaign(updated)
    }
  }

  if (stats.status !== Status.Sent) poll()
  return () => {
    timeoutId && clearTimeout(timeoutId)
  }
}, [id, stats.status, campaign, setCampaign])

To extend this further, can consider refactoring the stats fetching/polling logic in (Email|SMS|Telegram)Detail component into a custom hook.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That sounds good. I was also a little irked by the implication of de-sync. I'll explore your solution as well as the refactor.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, I also have some questions regarding possible de-sync of Campaign state between the frontend and backend at various steps during campaign creation. I'll look to message you during the weekdays about it :D

@zwliew zwliew added the frontend Frontend label Apr 9, 2021
@zwliew zwliew changed the title fix(campaign): set initial sentAt value when sending a campaign fix: re-fetch campaign details after campaign is sent Apr 14, 2021
console.error(
'sentAt is undefined. Using current Date() as a fallback.'
)
sentAtTime = new Date()
Copy link
Contributor Author

@zwliew zwliew Apr 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fallback doesn't seem necessary in practice, but I think it may be good as a form of defensive programming.

I think the only way !sentAt is true is if await exportCampaignStats(campaignId, campaignType) (1) returns before await getCampaignDetails(id) (2). This doesn't sound possible.

Copy link
Contributor

@lamkeewei lamkeewei left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm! Tested the following for each channel and works as expected:

  • Download delivery report without refresh on campaign details page
  • Download delivery report without refresh on dashboard page

@lamkeewei lamkeewei merged commit 381f1cf into develop Apr 14, 2021
@lamkeewei lamkeewei deleted the fix-report-invalid-date branch April 14, 2021 04:51
lamkeewei added a commit that referenced this pull request Apr 19, 2021
* develop:
  fix(docker/awscli): upgrade to python3 and install python3-dev (#1140)
  feat(frontend): set up test fixtures and deployment processes (#1086)
  feat: allow activation of SNS as fallback for all SMS campaigns (#1019)
  1.22.4
  fix: re-fetch campaign details after campaign is sent (#1116)
  chore: upgrade dependencies (#1120)
  chore: fix build warnings (#1103)
  fix(moment): disable periodic updates for react-moment components (#1127)
  fix: avoid calling setState when components are unmounted (#1109)
  chore: add callback server (#1112)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
frontend Frontend
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Initial download of delivery report has invalid date in the filename
2 participants