Skip to content

Commit 50f3c79

Browse files
authored
feat: (polls) support closePoll, add/fix missing fields (#1275)
1 parent c29447a commit 50f3c79

File tree

2 files changed

+46
-12
lines changed

2 files changed

+46
-12
lines changed

src/client.ts

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3430,7 +3430,7 @@ export class StreamChat<StreamChatGenerics extends ExtendableGenerics = DefaultG
34303430
/**
34313431
* Creates a poll
34323432
* @param params PollData The poll that will be created
3433-
* @returns {APIResponse & PollResponse} The poll
3433+
* @returns {APIResponse & CreatePollAPIResponse} The poll
34343434
*/
34353435
async createPoll(poll: PollData) {
34363436
return await this.post<APIResponse & CreatePollAPIResponse>(this.baseURL + `/polls`, poll);
@@ -3439,9 +3439,9 @@ export class StreamChat<StreamChatGenerics extends ExtendableGenerics = DefaultG
34393439
/**
34403440
* Retrieves a poll
34413441
* @param id string The poll id
3442-
* @returns {APIResponse & PollResponse} The poll
3442+
* @returns {APIResponse & GetPollAPIResponse} The poll
34433443
*/
3444-
async getPoll(id: string, userId?: string) {
3444+
async getPoll(id: string, userId?: string): Promise<APIResponse & GetPollAPIResponse> {
34453445
return await this.get<APIResponse & GetPollAPIResponse>(this.baseURL + `/polls/${id}`, {
34463446
...(userId ? { user_id: userId } : {}),
34473447
});
@@ -3457,13 +3457,16 @@ export class StreamChat<StreamChatGenerics extends ExtendableGenerics = DefaultG
34573457
}
34583458

34593459
/**
3460-
* Partically updates a poll
3460+
* Partially updates a poll
34613461
* @param id string The poll id
34623462
* @param {PartialPollUpdate<StreamChatGenerics>} partialPollObject which should contain id and any of "set" or "unset" params;
34633463
* example: {id: "44f26af5-f2be-4fa7-9dac-71cf893781de", set:{field: value}, unset:["field2"]}
3464-
* @returns {APIResponse & PollResponse} The poll
3464+
* @returns {APIResponse & UpdatePollAPIResponse} The poll
34653465
*/
3466-
async partialUpdatePoll(id: string, partialPollObject: PartialPollUpdate) {
3466+
async partialUpdatePoll(
3467+
id: string,
3468+
partialPollObject: PartialPollUpdate,
3469+
): Promise<APIResponse & UpdatePollAPIResponse> {
34673470
return await this.patch<APIResponse & UpdatePollAPIResponse>(this.baseURL + `/polls/${id}`, partialPollObject);
34683471
}
34693472

@@ -3479,6 +3482,19 @@ export class StreamChat<StreamChatGenerics extends ExtendableGenerics = DefaultG
34793482
});
34803483
}
34813484

3485+
/**
3486+
* Close a poll
3487+
* @param id string The poll id
3488+
* @returns {APIResponse & UpdatePollAPIResponse} The poll
3489+
*/
3490+
async closePoll(id: string): Promise<APIResponse & UpdatePollAPIResponse> {
3491+
return this.partialUpdatePoll(id, {
3492+
set: {
3493+
is_closed: true,
3494+
}
3495+
});
3496+
}
3497+
34823498
/**
34833499
* Creates a poll option
34843500
* @param pollId string The poll id
@@ -3525,10 +3541,11 @@ export class StreamChat<StreamChatGenerics extends ExtendableGenerics = DefaultG
35253541
}
35263542

35273543
/**
3528-
* Cast or cancel one or more votes on a poll
3544+
* Cast vote on a poll
3545+
* @param messageId string The message id
35293546
* @param pollId string The poll id
3530-
* @param votes PollVoteData[] The votes that will be casted (or canceled in case of an empty array)
3531-
* @returns {APIResponse & PollVotesAPIResponse} The poll votes
3547+
* @param vote PollVoteData The vote that will be casted
3548+
* @returns {APIResponse & CastVoteAPIResponse} The poll vote
35323549
*/
35333550
async castPollVote(messageId: string, pollId: string, vote: PollVoteData, options = {}) {
35343551
return await this.post<APIResponse & CastVoteAPIResponse>(
@@ -3537,6 +3554,18 @@ export class StreamChat<StreamChatGenerics extends ExtendableGenerics = DefaultG
35373554
);
35383555
}
35393556

3557+
/**
3558+
* Add a poll answer
3559+
* @param messageId string The message id
3560+
* @param pollId string The poll id
3561+
* @param answerText string The answer text
3562+
*/
3563+
async addPollAnswer(messageId: string, pollId: string, answerText: string) {
3564+
return this.castPollVote(messageId, pollId, {
3565+
answer_text: answerText
3566+
});
3567+
}
3568+
35403569
async removePollVote(messageId: string, pollId: string, voteId: string) {
35413570
return await this.delete<APIResponse & { vote: PollVote }>(
35423571
this.baseURL + `/messages/${messageId}/polls/${pollId}/vote/${voteId}`,

src/types.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1417,7 +1417,7 @@ export type ChannelFilters<StreamChatGenerics extends ExtendableGenerics = Defau
14171417
export type QueryPollsOptions = Pager;
14181418

14191419
export type VotesFiltersOptions = {
1420-
is_user_suggestion?: boolean;
1420+
is_answer?: boolean;
14211421
option_id?: string;
14221422
user_id?: string;
14231423
};
@@ -1445,6 +1445,10 @@ export type QueryPollsFilters = QueryFilters<
14451445
allow_answers?:
14461446
| RequireOnlyOne<Pick<QueryFilter<PollResponse['allow_answers']>, '$eq'>>
14471447
| PrimitiveFilter<PollResponse['allow_answers']>;
1448+
} & {
1449+
allow_user_suggested_options?:
1450+
| RequireOnlyOne<Pick<QueryFilter<PollResponse['allow_user_suggested_options']>, '$eq'>>
1451+
| PrimitiveFilter<PollResponse['allow_user_suggested_options']>;
14481452
} & {
14491453
voting_visibility?:
14501454
| RequireOnlyOne<Pick<QueryFilter<PollResponse['voting_visibility']>, '$eq'>>
@@ -1477,8 +1481,8 @@ export type QueryVotesFilters = QueryFilters<
14771481
| PrimitiveFilter<VotesFiltersOptions['option_id']>;
14781482
} & {
14791483
is_answer?:
1480-
| RequireOnlyOne<Pick<QueryFilter<VotesFiltersOptions['is_user_suggestion']>, '$eq'>>
1481-
| PrimitiveFilter<VotesFiltersOptions['is_user_suggestion']>;
1484+
| RequireOnlyOne<Pick<QueryFilter<VotesFiltersOptions['is_answer']>, '$eq'>>
1485+
| PrimitiveFilter<VotesFiltersOptions['is_answer']>;
14821486
} & {
14831487
user_id?:
14841488
| RequireOnlyOne<Pick<QueryFilter<VotesFiltersOptions['user_id']>, '$eq' | '$in'>>
@@ -2900,6 +2904,7 @@ export type UpdatePollAPIResponse<StreamChatGenerics extends ExtendableGenerics
29002904
export type PollResponse<
29012905
StreamChatGenerics extends ExtendableGenerics = DefaultGenerics
29022906
> = StreamChatGenerics['pollType'] & {
2907+
answers_count: number;
29032908
created_at: string;
29042909
created_by: UserResponse<StreamChatGenerics> | null;
29052910
created_by_id: string;

0 commit comments

Comments
 (0)