Skip to content

Commit

Permalink
fix: migrate pollService to TS
Browse files Browse the repository at this point in the history
Signed-off-by: Maksim Sukharev <[email protected]>
  • Loading branch information
Antreesy committed Oct 4, 2024
1 parent 06b85d8 commit 17001f8
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 79 deletions.
65 changes: 0 additions & 65 deletions src/services/pollService.js

This file was deleted.

69 changes: 69 additions & 0 deletions src/services/pollService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
* SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import axios from '@nextcloud/axios'
import { generateOcsUrl } from '@nextcloud/router'

import type {
closePollResponse,
createPollParams,
createPollResponse,
getPollResponse,
votePollParams,
votePollResponse,
} from '../types/index.ts'

type createPollPayload = { token: string } & createPollParams

/**
* @param payload The payload
* @param payload.token The conversation token
* @param payload.question The question of the poll
* @param payload.options The options participants can vote for
* @param payload.resultMode Result mode of the poll (0 - always visible | 1 - hidden until the poll is closed)
* @param payload.maxVotes Maximum amount of options a user can vote for (0 - unlimited | 1 - single answer)
*/
const createPoll = async ({ token, question, options, resultMode, maxVotes }: createPollPayload): createPollResponse => {
return axios.post(generateOcsUrl('apps/spreed/api/v1/poll/{token}', { token }), {
question,
options,
resultMode,
maxVotes,
} as createPollParams)
}

/**
* @param token The conversation token
* @param pollId Id of the poll
*/
const getPollData = async (token: string, pollId: number): getPollResponse => {
return axios.get(generateOcsUrl('apps/spreed/api/v1/poll/{token}/{pollId}', { token, pollId }))
}

/**
* @param token The conversation token
* @param pollId Id of the poll
* @param optionIds Indexes of options the participant votes for
*/
const submitVote = async (token: string, pollId: number, optionIds: votePollParams['optionIds']): votePollResponse => {
return axios.post(generateOcsUrl('apps/spreed/api/v1/poll/{token}/{pollId}', { token, pollId }), {
optionIds,
} as votePollParams)
}

/**
* @param token The conversation token
* @param pollId Id of the poll
*/
const endPoll = async (token: string, pollId: number): closePollResponse => {
return axios.delete(generateOcsUrl('apps/spreed/api/v1/poll/{token}/{pollId}', { token, pollId }))
}

export {
createPoll,
getPollData,
submitVote,
endPoll,
}
21 changes: 13 additions & 8 deletions src/stores/__tests__/polls.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@ import flushPromises from 'flush-promises'
import { setActivePinia, createPinia } from 'pinia'

import { ATTENDEE } from '../../constants.js'
import pollService from '../../services/pollService.js'
import {
createPoll,
getPollData,
submitVote,
endPoll,
} from '../../services/pollService.ts'
import { generateOCSResponse } from '../../test-helpers.js'
import { usePollsStore } from '../polls.js'

jest.mock('../../services/pollService.js', () => ({
postNewPoll: jest.fn(),
jest.mock('../../services/pollService', () => ({
createPoll: jest.fn(),
getPollData: jest.fn(),
submitVote: jest.fn(),
endPoll: jest.fn(),
Expand Down Expand Up @@ -88,7 +93,7 @@ describe('pollsStore', () => {
it('receives a poll from server and adds it to the store', async () => {
// Arrange
const response = generateOCSResponse({ payload: poll })
pollService.getPollData.mockResolvedValue(response)
getPollData.mockResolvedValue(response)

// Act
await pollsStore.getPollData({ token: TOKEN, pollId: poll.id })
Expand All @@ -101,7 +106,7 @@ describe('pollsStore', () => {
// Arrange
jest.useFakeTimers()
const response = generateOCSResponse({ payload: poll })
pollService.getPollData.mockResolvedValue(response)
getPollData.mockResolvedValue(response)

// Act
pollsStore.debounceGetPollData({ token: TOKEN, pollId: poll.id })
Expand All @@ -116,7 +121,7 @@ describe('pollsStore', () => {
it('creates a poll and adds it to the store', async () => {
// Arrange
const response = generateOCSResponse({ payload: poll })
pollService.postNewPoll.mockResolvedValue(response)
createPoll.mockResolvedValue(response)

// Act
await pollsStore.createPoll({ token: TOKEN, ...pollRequest })
Expand All @@ -129,7 +134,7 @@ describe('pollsStore', () => {
// Arrange
pollsStore.addPoll({ token: TOKEN, poll })
const response = generateOCSResponse({ payload: pollWithVote })
pollService.submitVote.mockResolvedValue(response)
submitVote.mockResolvedValue(response)

// Act
await pollsStore.submitVote({ token: TOKEN, pollId: poll.id, optionIds: [0] })
Expand All @@ -142,7 +147,7 @@ describe('pollsStore', () => {
// Arrange
pollsStore.addPoll({ token: TOKEN, poll: pollWithVote })
const response = generateOCSResponse({ payload: pollWithVoteEnded })
pollService.endPoll.mockResolvedValue(response)
endPoll.mockResolvedValue(response)

// Act
await pollsStore.endPoll({ token: TOKEN, pollId: poll.id })
Expand Down
17 changes: 11 additions & 6 deletions src/stores/polls.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ import Vue from 'vue'
import { showError, showInfo, TOAST_PERMANENT_TIMEOUT } from '@nextcloud/dialogs'
import { t } from '@nextcloud/l10n'

import pollService from '../services/pollService.js'
import {
createPoll,
getPollData,
submitVote,
endPoll,
} from '../services/pollService.ts'

export const usePollsStore = defineStore('polls', {
state: () => ({
Expand Down Expand Up @@ -39,7 +44,7 @@ export const usePollsStore = defineStore('polls', {

async getPollData({ token, pollId }) {
try {
const response = await pollService.getPollData(token, pollId)
const response = await getPollData(token, pollId)
this.addPoll({ token, poll: response.data.ocs.data })
} catch (error) {
console.error(error)
Expand Down Expand Up @@ -72,13 +77,13 @@ export const usePollsStore = defineStore('polls', {

async createPoll({ token, question, options, resultMode, maxVotes }) {
try {
const response = await pollService.postNewPoll(
const response = await createPoll({
token,
question,
options,
resultMode,
maxVotes,
)
})
this.addPoll({ token, poll: response.data.ocs.data })

return response.data.ocs.data
Expand All @@ -89,7 +94,7 @@ export const usePollsStore = defineStore('polls', {

async submitVote({ token, pollId, optionIds }) {
try {
const response = await pollService.submitVote(token, pollId, optionIds)
const response = await submitVote(token, pollId, optionIds)
this.addPoll({ token, poll: response.data.ocs.data })
} catch (error) {
console.error(error)
Expand All @@ -99,7 +104,7 @@ export const usePollsStore = defineStore('polls', {

async endPoll({ token, pollId }) {
try {
const response = await pollService.endPoll(token, pollId)
const response = await endPoll(token, pollId)
this.addPoll({ token, poll: response.data.ocs.data })
} catch (error) {
console.error(error)
Expand Down
10 changes: 10 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,13 @@ export type requestAssistanceResponse = ApiResponse<operations['breakout_room-re
export type resetRequestAssistanceResponse = ApiResponse<operations['breakout_room-reset-request-for-assistance']['responses'][200]['content']['application/json']>
export type switchToBreakoutRoomParams = operations['breakout_room-switch-breakout-room']['requestBody']['content']['application/json']
export type switchToBreakoutRoomResponse = ApiResponse<operations['breakout_room-switch-breakout-room']['responses'][200]['content']['application/json']>

// Polls
export type Poll = components['schemas']['Poll']

export type getPollResponse = ApiResponse<operations['poll-show-poll']['responses'][200]['content']['application/json']>
export type createPollParams = operations['poll-create-poll']['requestBody']['content']['application/json']
export type createPollResponse = ApiResponse<operations['poll-create-poll']['responses'][201]['content']['application/json']>
export type votePollParams = Required<operations['poll-vote-poll']>['requestBody']['content']['application/json']
export type votePollResponse = ApiResponse<operations['poll-vote-poll']['responses'][200]['content']['application/json']>
export type closePollResponse = ApiResponse<operations['poll-close-poll']['responses'][200]['content']['application/json']>

0 comments on commit 17001f8

Please sign in to comment.