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

Move Notification Logic from Discussion Store to Firebase Functions #3620

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions functions/src/Utils/db.utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { getDiscussionCollectionName, randomID } from './db.utils'

describe('getDiscussionCollectionName', () => {
it('should return "questions" when sourceType is "question"', () => {
const result = getDiscussionCollectionName('question')
expect(result).toBe('questions')
})

it('should return "howtos" when sourceType is "howto"', () => {
const result = getDiscussionCollectionName('howto')
expect(result).toBe('howtos')
})

it('should return null when sourceType is unrecognized', () => {
const result = getDiscussionCollectionName('unknown')
expect(result).toBe(null)
})
it('should return null when sourceType is an empty string', () => {
const result = getDiscussionCollectionName('')
expect(result).toBeNull()
})

it('should return "research" when sourceType is "researchUpdate"', () => {
const result = getDiscussionCollectionName('researchUpdate')
expect(result).toBe('research')
})
})

describe('randomID', () => {
// generates a string of length 20
it('should generate a string of length 20 and include only alphanumeric characters', () => {
const id = randomID()
const alphanumericRegex = /^[a-zA-Z0-9]+$/
expect(id).toHaveLength(20)
expect(alphanumericRegex.test(id)).toBe(true)
})
})
31 changes: 31 additions & 0 deletions functions/src/Utils/db.utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
export type DiscussionEndpoints = 'howtos' | 'research' | 'questions'

/**
* Function used to determine the discussion collection name based on the source type.
*/
export const getDiscussionCollectionName = (
sourceType: string,
): DiscussionEndpoints | null => {
switch (sourceType) {
case 'question':
return 'questions'
case 'howto':
return 'howtos'
case 'researchUpdate':
return 'research'
default:
return null
}
}

/**
* Function used to generate random ID in same manner as firestore
*/
export const randomID = () => {
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
let autoId = ''
for (let i = 0; i < 20; i++) {
autoId += chars.charAt(Math.floor(Math.random() * chars.length))
}
return autoId
}
goratt12 marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions functions/src/Utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './auth.utils'
export * from './data.utils'
export * from './file.utils'
export * from './db.utils'
Loading
Loading