|
1 | | -import type { PollResponse } from 'stream-chat'; |
| 1 | +import { isVoteAnswer, PollAnswer, PollResponse, PollVote } from 'stream-chat'; |
2 | 2 |
|
| 3 | +import { DefaultStreamChatGenerics } from '../../types/types'; |
| 4 | +import { mapPollToStorable } from '../mappers/mapPollToStorable'; |
| 5 | +import { mapStorableToPoll } from '../mappers/mapStorableToPoll'; |
3 | 6 | import { QuickSqliteClient } from '../QuickSqliteClient'; |
4 | 7 | import { createSelectQuery } from '../sqlite-utils/createSelectQuery'; |
5 | 8 | import { createUpdateQuery } from '../sqlite-utils/createUpdateQuery'; |
6 | 9 | import type { PreparedQueries } from '../types'; |
7 | 10 |
|
8 | | -export const updatePollMessage = ({ |
| 11 | +export const updatePollMessage = < |
| 12 | + StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics, |
| 13 | +>({ |
| 14 | + eventType, |
9 | 15 | flush = true, |
10 | 16 | poll, |
| 17 | + poll_vote, |
| 18 | + userID, |
11 | 19 | }: { |
12 | | - poll: PollResponse; |
| 20 | + eventType: string; |
| 21 | + poll: PollResponse<StreamChatGenerics>; |
| 22 | + userID: string; |
13 | 23 | flush?: boolean; |
| 24 | + poll_vote?: PollVote<StreamChatGenerics> | PollAnswer<StreamChatGenerics>; |
14 | 25 | }) => { |
15 | 26 | const queries: PreparedQueries[] = []; |
16 | 27 |
|
17 | | - const messagesWithPoll = QuickSqliteClient.executeSql.apply( |
| 28 | + const pollsFromDB = QuickSqliteClient.executeSql.apply( |
18 | 29 | null, |
19 | | - createSelectQuery('messages', ['*'], { |
20 | | - poll_id: poll.id, |
| 30 | + createSelectQuery('poll', ['*'], { |
| 31 | + id: poll.id, |
21 | 32 | }), |
22 | 33 | ); |
23 | 34 |
|
24 | | - for (const message of messagesWithPoll) { |
25 | | - const storablePoll = JSON.stringify({ |
| 35 | + for (const pollFromDB of pollsFromDB) { |
| 36 | + const serializedPoll = mapStorableToPoll(pollFromDB); |
| 37 | + const { latest_answers = [], own_votes = [] } = serializedPoll; |
| 38 | + let newOwnVotes = own_votes; |
| 39 | + if (poll_vote && poll_vote.user?.id === userID) { |
| 40 | + newOwnVotes = |
| 41 | + eventType === 'poll.vote_removed' |
| 42 | + ? newOwnVotes.filter((vote) => vote.id !== poll_vote.id) |
| 43 | + : [poll_vote, ...newOwnVotes.filter((vote) => vote.id !== poll_vote.id)]; |
| 44 | + } |
| 45 | + let newLatestAnswers = latest_answers; |
| 46 | + if (poll_vote && isVoteAnswer(poll_vote)) { |
| 47 | + newLatestAnswers = |
| 48 | + eventType === 'poll.vote_removed' |
| 49 | + ? newLatestAnswers.filter((answer) => answer.id !== poll_vote?.id) |
| 50 | + : [poll_vote, ...newLatestAnswers.filter((answer) => answer.id !== poll_vote?.id)]; |
| 51 | + } |
| 52 | + |
| 53 | + const storablePoll = mapPollToStorable({ |
26 | 54 | ...poll, |
27 | | - latest_votes: message.poll.latest_votes, |
28 | | - own_votes: message.poll.own_votes, |
| 55 | + latest_answers: newLatestAnswers, |
| 56 | + own_votes: newOwnVotes, |
29 | 57 | }); |
30 | | - const storableMessage = { ...message, poll: storablePoll }; |
31 | 58 |
|
32 | 59 | queries.push( |
33 | | - createUpdateQuery('messages', storableMessage, { |
34 | | - id: message.id, |
| 60 | + createUpdateQuery('poll', storablePoll, { |
| 61 | + id: poll.id, |
35 | 62 | }), |
36 | 63 | ); |
37 | 64 | QuickSqliteClient.logger?.('info', 'updatePoll', { |
38 | | - message: storableMessage, |
39 | 65 | poll: storablePoll, |
40 | 66 | }); |
41 | 67 | } |
|
0 commit comments