Skip to content

Commit

Permalink
fix: saving a research article as a draft does not trigger webhook (O…
Browse files Browse the repository at this point in the history
  • Loading branch information
onim-at authored Aug 31, 2024
1 parent 3ee0a8c commit ab4b347
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
34 changes: 34 additions & 0 deletions functions/src/Integrations/firebase-discord.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ResearchUpdateStatus } from 'oa-shared'
import { handleResearchUpdatePublished } from './firebase-discord'

import type { SimpleResearchArticle } from './firebase-discord'
Expand Down Expand Up @@ -72,4 +73,37 @@ describe('handle research article update change', () => {
)
expect(wasMockSendMessagedCalled).toEqual(false)
})

it('should not send message when update is a draft', () => {
const webhookUrl = 'exmaple.com'
const previousContent: SimpleResearchArticle = {
slug: 'test',
updates: [],
}
const newContent: SimpleResearchArticle = {
slug: 'test',
updates: [
{
_id: 'bobmore',
title: 'test',
collaborators: ['Bob'],
status: ResearchUpdateStatus.DRAFT,
},
],
}

let wasMockSendMessagedCalled = false
const mockSendMessage = (_: string): Promise<any> => {
wasMockSendMessagedCalled = true
return Promise.resolve()
}

handleResearchUpdatePublished(
webhookUrl,
previousContent,
newContent,
mockSendMessage,
)
expect(wasMockSendMessagedCalled).toEqual(false)
})
})
8 changes: 7 additions & 1 deletion functions/src/Integrations/firebase-discord.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import axios from 'axios'
import * as functions from 'firebase-functions'
import { IModerationStatus } from 'oa-shared'
import { IModerationStatus, ResearchUpdateStatus } from 'oa-shared'

import { CONFIG } from '../config/config'

Expand Down Expand Up @@ -78,6 +78,7 @@ interface SimpleResearchArticleUpdate {
_id: string
title: string
collaborators?: string[]
status?: ResearchUpdateStatus
}

export async function handleResearchUpdatePublished(
Expand All @@ -99,6 +100,11 @@ export async function handleResearchUpdatePublished(
const newUpdateIndex = updatedContent.updates.length - 1
const newUpdate = updatedContent.updates[newUpdateIndex]

if (newUpdate.status === ResearchUpdateStatus.DRAFT) {
console.log('Update is a draft')
return
}

// On Research Updates, we actually expect the collaborators to be a single person
// but it is a list.
// source:
Expand Down

0 comments on commit ab4b347

Please sign in to comment.