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: discussion duplication #3993

Merged
merged 2 commits into from
Nov 7, 2024
Merged
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
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
Loading