From 0a4864c37a51966290488152791762f581f865d3 Mon Sep 17 00:00:00 2001 From: Guilherme Gazzo Date: Wed, 25 May 2022 13:30:39 -0300 Subject: [PATCH] move --- .../app/api/server/lib/custom-sounds.js | 20 ------- .../meteor/app/api/server/v1/custom-sounds.js | 26 --------- .../meteor/app/api/server/v1/custom-sounds.ts | 29 ++++++++++ ...m-user-status.js => custom-user-status.ts} | 55 ++++++++++--------- .../app/models/server/raw/CustomUserStatus.ts | 7 ++- packages/rest-typings/src/v1/customSounds.ts | 7 +-- .../rest-typings/src/v1/customUserStatus.ts | 15 ++++- 7 files changed, 80 insertions(+), 79 deletions(-) delete mode 100644 apps/meteor/app/api/server/lib/custom-sounds.js delete mode 100644 apps/meteor/app/api/server/v1/custom-sounds.js create mode 100644 apps/meteor/app/api/server/v1/custom-sounds.ts rename apps/meteor/app/api/server/v1/{custom-user-status.js => custom-user-status.ts} (60%) diff --git a/apps/meteor/app/api/server/lib/custom-sounds.js b/apps/meteor/app/api/server/lib/custom-sounds.js deleted file mode 100644 index 0579e99f38e7d..0000000000000 --- a/apps/meteor/app/api/server/lib/custom-sounds.js +++ /dev/null @@ -1,20 +0,0 @@ -import { CustomSounds } from '../../../models/server/raw'; - -export async function findCustomSounds({ query = {}, pagination: { offset, count, sort } }) { - const cursor = await CustomSounds.find(query, { - sort: sort || { name: 1 }, - skip: offset, - limit: count, - }); - - const total = await cursor.count(); - - const sounds = await cursor.toArray(); - - return { - sounds, - count: sounds.length, - offset, - total, - }; -} diff --git a/apps/meteor/app/api/server/v1/custom-sounds.js b/apps/meteor/app/api/server/v1/custom-sounds.js deleted file mode 100644 index 57c5ac9a0c7f9..0000000000000 --- a/apps/meteor/app/api/server/v1/custom-sounds.js +++ /dev/null @@ -1,26 +0,0 @@ -import { API } from '../api'; -import { findCustomSounds } from '../lib/custom-sounds'; - -API.v1.addRoute( - 'custom-sounds.list', - { authRequired: true }, - { - get() { - const { offset, count } = this.getPaginationItems(); - const { sort, query } = this.parseJsonQuery(); - - return API.v1.success( - Promise.await( - findCustomSounds({ - query, - pagination: { - offset, - count, - sort, - }, - }), - ), - ); - }, - }, -); diff --git a/apps/meteor/app/api/server/v1/custom-sounds.ts b/apps/meteor/app/api/server/v1/custom-sounds.ts new file mode 100644 index 0000000000000..f6294c8a5ec13 --- /dev/null +++ b/apps/meteor/app/api/server/v1/custom-sounds.ts @@ -0,0 +1,29 @@ +import { CustomSounds } from '../../../models/server/raw'; +import { API } from '../api'; + +API.v1.addRoute( + 'custom-sounds.list', + { authRequired: true }, + { + async get() { + const { offset, count } = this.getPaginationItems(); + const { sort, query } = this.parseJsonQuery(); + const cursor = await CustomSounds.find(query, { + sort: sort || { name: 1 }, + skip: offset, + limit: count, + }); + + const total = await cursor.count(); + + const sounds = await cursor.toArray(); + + return API.v1.success({ + sounds, + count: sounds.length, + offset, + total, + }); + }, + }, +); diff --git a/apps/meteor/app/api/server/v1/custom-user-status.js b/apps/meteor/app/api/server/v1/custom-user-status.ts similarity index 60% rename from apps/meteor/app/api/server/v1/custom-user-status.js rename to apps/meteor/app/api/server/v1/custom-user-status.ts index b53583a4dc0b6..e6857143d8f95 100644 --- a/apps/meteor/app/api/server/v1/custom-user-status.js +++ b/apps/meteor/app/api/server/v1/custom-user-status.ts @@ -1,7 +1,7 @@ import { Meteor } from 'meteor/meteor'; import { Match, check } from 'meteor/check'; -import { CustomUserStatus } from '../../../models'; +import { CustomUserStatus } from '../../../models/server/raw'; import { API } from '../api'; import { findCustomUserStatus } from '../lib/custom-user-status'; @@ -9,21 +9,19 @@ API.v1.addRoute( 'custom-user-status.list', { authRequired: true }, { - get() { + async get() { const { offset, count } = this.getPaginationItems(); const { sort, query } = this.parseJsonQuery(); return API.v1.success( - Promise.await( - findCustomUserStatus({ - query, - pagination: { - offset, - count, - sort, - }, - }), - ), + await findCustomUserStatus({ + query, + pagination: { + offset, + count, + sort, + }, + }), ); }, }, @@ -33,7 +31,7 @@ API.v1.addRoute( 'custom-user-status.create', { authRequired: true }, { - post() { + async post() { check(this.bodyParams, { name: String, statusType: Match.Maybe(String), @@ -44,12 +42,15 @@ API.v1.addRoute( statusType: this.bodyParams.statusType, }; - Meteor.runAsUser(this.userId, () => { - Meteor.call('insertOrUpdateUserStatus', userStatusData); - }); + Meteor.call('insertOrUpdateUserStatus', userStatusData); + + const customUserStatus = await CustomUserStatus.findOneByName(userStatusData.name); + if (!customUserStatus) { + throw new Meteor.Error('error-creating-custom-user-status', 'Error creating custom user status'); + } return API.v1.success({ - customUserStatus: CustomUserStatus.findOneByName(userStatusData.name), + customUserStatus, }); }, }, @@ -65,7 +66,7 @@ API.v1.addRoute( return API.v1.failure('The "customUserStatusId" params is required!'); } - Meteor.runAsUser(this.userId, () => Meteor.call('deleteCustomUserStatus', customUserStatusId)); + Meteor.call('deleteCustomUserStatus', customUserStatusId); return API.v1.success(); }, @@ -76,7 +77,7 @@ API.v1.addRoute( 'custom-user-status.update', { authRequired: true }, { - post() { + async post() { check(this.bodyParams, { _id: String, name: String, @@ -89,19 +90,23 @@ API.v1.addRoute( statusType: this.bodyParams.statusType, }; - const customUserStatus = CustomUserStatus.findOneById(userStatusData._id); + const customUserStatusToUpdate = await CustomUserStatus.findOneById(userStatusData._id); // Ensure the message exists - if (!customUserStatus) { + if (!customUserStatusToUpdate) { return API.v1.failure(`No custom user status found with the id of "${userStatusData._id}".`); } - Meteor.runAsUser(this.userId, () => { - Meteor.call('insertOrUpdateUserStatus', userStatusData); - }); + Meteor.call('insertOrUpdateUserStatus', userStatusData); + + const customUserStatus = await CustomUserStatus.findOneById(userStatusData._id); + + if (!customUserStatus) { + throw new Meteor.Error('error-updating-custom-user-status', 'Error updating custom user status'); + } return API.v1.success({ - customUserStatus: CustomUserStatus.findOneById(userStatusData._id), + customUserStatus, }); }, }, diff --git a/apps/meteor/app/models/server/raw/CustomUserStatus.ts b/apps/meteor/app/models/server/raw/CustomUserStatus.ts index be94faa2833a8..d23dc8fd5a138 100644 --- a/apps/meteor/app/models/server/raw/CustomUserStatus.ts +++ b/apps/meteor/app/models/server/raw/CustomUserStatus.ts @@ -9,8 +9,11 @@ export class CustomUserStatusRaw extends BaseRaw { } // find one by name - async findOneByName(name: string, options: WithoutProjection>): Promise { - return this.findOne({ name }, options); + + async findOneByName(name: string, options?: undefined): Promise; + + async findOneByName(name: string, options?: WithoutProjection>): Promise { + return options ? this.findOne({ name }, options) : this.findOne({ name }); } // find diff --git a/packages/rest-typings/src/v1/customSounds.ts b/packages/rest-typings/src/v1/customSounds.ts index 4a8f75a680898..1f39263abb673 100644 --- a/packages/rest-typings/src/v1/customSounds.ts +++ b/packages/rest-typings/src/v1/customSounds.ts @@ -1,14 +1,11 @@ +import type { ICustomSound } from '../../../core-typings/dist'; import type { PaginatedRequest } from '../helpers/PaginatedRequest'; import type { PaginatedResult } from '../helpers/PaginatedResult'; export type CustomSoundEndpoint = { 'custom-sounds.list': { GET: (params: PaginatedRequest<{ query: string }>) => PaginatedResult<{ - sounds: { - _id: string; - name: string; - extension: string; - }[]; + sounds: ICustomSound[]; }>; }; }; diff --git a/packages/rest-typings/src/v1/customUserStatus.ts b/packages/rest-typings/src/v1/customUserStatus.ts index 9f9f8350180f6..943aebb9fdcac 100644 --- a/packages/rest-typings/src/v1/customUserStatus.ts +++ b/packages/rest-typings/src/v1/customUserStatus.ts @@ -1,4 +1,4 @@ -import type { IUserStatus } from '@rocket.chat/core-typings'; +import type { ICustomUserStatus, IUserStatus } from '@rocket.chat/core-typings'; import type { PaginatedRequest } from '../helpers/PaginatedRequest'; import type { PaginatedResult } from '../helpers/PaginatedResult'; @@ -9,4 +9,17 @@ export type CustomUserStatusEndpoints = { statuses: IUserStatus[]; }>; }; + 'custom-user-status.create': { + POST: (params: { name: string; statusType?: string }) => { + customUserStatus: ICustomUserStatus; + }; + }; + 'custom-user-status.delete': { + POST: (params: { customUserStatusId: string }) => void; + }; + 'custom-user-status.update': { + POST: (params: { id: string; name?: string; statusType?: string }) => { + customUserStatus: ICustomUserStatus; + }; + }; };