Skip to content

Commit

Permalink
fix: discussion duplication (#3993)
Browse files Browse the repository at this point in the history
  • Loading branch information
mariojsnunes authored Nov 7, 2024
1 parent 69bd593 commit 6bfb035
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 25 deletions.
18 changes: 0 additions & 18 deletions src/stores/Discussions/discussion.store.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { describe, expect, it, vi } from 'vitest'

vi.mock('../common/module.store')
import { faker } from '@faker-js/faker'
import {
FactoryDiscussion,
FactoryDiscussionComment,
Expand Down Expand Up @@ -67,23 +66,6 @@ const factory = (

describe('discussion.store', () => {
describe('fetchOrCreateDiscussionBySource', () => {
it('fetches a discussion by sourceId', async () => {
const fakeSourceId = faker.internet.password()
const fakePrimaryContentId = faker.internet.password()
const { store, getWhereFn } = factory([
FactoryDiscussion({ sourceId: fakeSourceId }),
])

await store.fetchOrCreateDiscussionBySource(
fakeSourceId,
'question',
fakePrimaryContentId,
)

expect(getWhereFn).toHaveBeenCalledTimes(1)
expect(getWhereFn).toHaveBeenCalledWith('sourceId', '==', fakeSourceId)
})

it('creates a discussion if one does not exist', async () => {
const { store, getWhereFn, setFn } = factory()

Expand Down
16 changes: 9 additions & 7 deletions src/stores/Discussions/discussions.store.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,17 @@ export class DiscussionStore extends ModuleStore {
sourceType: IDiscussion['sourceType'],
primaryContentId: IDiscussion['primaryContentId'],
): Promise<IDiscussionDB | null> {
const foundDiscussion =
toJS(
await this.db
.collection<IDiscussion>(DISCUSSIONS_COLLECTION)
.getWhere('sourceId', '==', sourceId),
)[0] || null
const discussions = toJS(
await this.db
.collection<IDiscussion>(DISCUSSIONS_COLLECTION)
.getWhere('sourceId', '==', sourceId, 1),
)

const foundDiscussion = discussions
? discussions.find((x) => x.comments.length > 0) || discussions.at(0)
: null

if (foundDiscussion) {
await this._validatePrimaryContentId(foundDiscussion, primaryContentId)
return this._formatDiscussion(foundDiscussion)
}

Expand Down

0 comments on commit 6bfb035

Please sign in to comment.