Skip to content

feat: (polls) support closePoll, add/fix missing fields #1275

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

Merged
merged 1 commit into from
Apr 9, 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
47 changes: 38 additions & 9 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3430,7 +3430,7 @@ export class StreamChat<StreamChatGenerics extends ExtendableGenerics = DefaultG
/**
* Creates a poll
* @param params PollData The poll that will be created
* @returns {APIResponse & PollResponse} The poll
* @returns {APIResponse & CreatePollAPIResponse} The poll
*/
async createPoll(poll: PollData) {
return await this.post<APIResponse & CreatePollAPIResponse>(this.baseURL + `/polls`, poll);
Expand All @@ -3439,9 +3439,9 @@ export class StreamChat<StreamChatGenerics extends ExtendableGenerics = DefaultG
/**
* Retrieves a poll
* @param id string The poll id
* @returns {APIResponse & PollResponse} The poll
* @returns {APIResponse & GetPollAPIResponse} The poll
*/
async getPoll(id: string, userId?: string) {
async getPoll(id: string, userId?: string): Promise<APIResponse & GetPollAPIResponse> {
return await this.get<APIResponse & GetPollAPIResponse>(this.baseURL + `/polls/${id}`, {
...(userId ? { user_id: userId } : {}),
});
Expand All @@ -3457,13 +3457,16 @@ export class StreamChat<StreamChatGenerics extends ExtendableGenerics = DefaultG
}

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

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

/**
* Close a poll
* @param id string The poll id
* @returns {APIResponse & UpdatePollAPIResponse} The poll
*/
async closePoll(id: string): Promise<APIResponse & UpdatePollAPIResponse> {
return this.partialUpdatePoll(id, {
set: {
is_closed: true,
}
});
}

/**
* Creates a poll option
* @param pollId string The poll id
Expand Down Expand Up @@ -3525,10 +3541,11 @@ export class StreamChat<StreamChatGenerics extends ExtendableGenerics = DefaultG
}

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

/**
* Add a poll answer
* @param messageId string The message id
* @param pollId string The poll id
* @param answerText string The answer text
*/
async addPollAnswer(messageId: string, pollId: string, answerText: string) {
return this.castPollVote(messageId, pollId, {
answer_text: answerText
});
}

async removePollVote(messageId: string, pollId: string, voteId: string) {
return await this.delete<APIResponse & { vote: PollVote }>(
this.baseURL + `/messages/${messageId}/polls/${pollId}/vote/${voteId}`,
Expand Down
11 changes: 8 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1417,7 +1417,7 @@ export type ChannelFilters<StreamChatGenerics extends ExtendableGenerics = Defau
export type QueryPollsOptions = Pager;

export type VotesFiltersOptions = {
is_user_suggestion?: boolean;
is_answer?: boolean;
option_id?: string;
user_id?: string;
};
Expand Down Expand Up @@ -1445,6 +1445,10 @@ export type QueryPollsFilters = QueryFilters<
allow_answers?:
| RequireOnlyOne<Pick<QueryFilter<PollResponse['allow_answers']>, '$eq'>>
| PrimitiveFilter<PollResponse['allow_answers']>;
} & {
allow_user_suggested_options?:
| RequireOnlyOne<Pick<QueryFilter<PollResponse['allow_user_suggested_options']>, '$eq'>>
| PrimitiveFilter<PollResponse['allow_user_suggested_options']>;
} & {
voting_visibility?:
| RequireOnlyOne<Pick<QueryFilter<PollResponse['voting_visibility']>, '$eq'>>
Expand Down Expand Up @@ -1477,8 +1481,8 @@ export type QueryVotesFilters = QueryFilters<
| PrimitiveFilter<VotesFiltersOptions['option_id']>;
} & {
is_answer?:
| RequireOnlyOne<Pick<QueryFilter<VotesFiltersOptions['is_user_suggestion']>, '$eq'>>
| PrimitiveFilter<VotesFiltersOptions['is_user_suggestion']>;
| RequireOnlyOne<Pick<QueryFilter<VotesFiltersOptions['is_answer']>, '$eq'>>
| PrimitiveFilter<VotesFiltersOptions['is_answer']>;
} & {
user_id?:
| RequireOnlyOne<Pick<QueryFilter<VotesFiltersOptions['user_id']>, '$eq' | '$in'>>
Expand Down Expand Up @@ -2900,6 +2904,7 @@ export type UpdatePollAPIResponse<StreamChatGenerics extends ExtendableGenerics
export type PollResponse<
StreamChatGenerics extends ExtendableGenerics = DefaultGenerics
> = StreamChatGenerics['pollType'] & {
answers_count: number;
created_at: string;
created_by: UserResponse<StreamChatGenerics> | null;
created_by_id: string;
Expand Down
Loading