11import { api } from '@rocket.chat/core-services' ;
2+ import type { IWebdavAccount , IWebdavAccountIntegration } from '@rocket.chat/core-typings' ;
23import { WebdavAccounts } from '@rocket.chat/models' ;
3- import Ajv from 'ajv' ;
4+ import { ajv } from '@rocket.chat/rest-typings/src/v1/Ajv' ;
5+ import type { DeleteResult } from 'mongodb' ;
46
7+ import type { ExtractRoutesFromAPI } from '../ApiClass' ;
58import { API } from '../api' ;
69import { findWebdavAccountsByUserId } from '../lib/webdav' ;
710
8- // TO-DO: remove this AJV instance and import one from the core-typings
9- const ajv = new Ajv ( { coerceTypes : true } ) ;
11+ const webdavGetMyAccountsEndpoints = API . v1 . get (
12+ 'webdav.getMyAccounts' ,
13+ {
14+ authRequired : true ,
15+ response : {
16+ 200 : ajv . compile < {
17+ accounts : IWebdavAccountIntegration [ ] ;
18+ } > ( {
19+ type : 'object' ,
20+ properties : {
21+ accounts : {
22+ type : 'array' ,
23+ items : {
24+ type : 'object' ,
25+ properties : {
26+ _id : {
27+ type : 'string' ,
28+ } ,
29+ serverURL : {
30+ type : 'string' ,
31+ } ,
32+ username : {
33+ type : 'string' ,
34+ } ,
35+ name : {
36+ type : 'string' ,
37+ } ,
38+ } ,
39+ required : [ '_id' , 'serverURL' , 'username' , 'name' ] ,
40+ additionalProperties : false ,
41+ } ,
42+ } ,
43+ success : {
44+ type : 'boolean' ,
45+ description : 'Indicates if the request was successful.' ,
46+ } ,
47+ } ,
48+ required : [ 'success' , 'accounts' ] ,
49+ additionalProperties : false ,
50+ } ) ,
51+ 401 : ajv . compile ( {
52+ type : 'object' ,
53+ properties : {
54+ message : {
55+ type : 'string' ,
56+ } ,
57+ success : {
58+ type : 'boolean' ,
59+ description : 'Indicates if the request was successful.' ,
60+ } ,
61+ } ,
62+ required : [ 'success' , 'message' ] ,
63+ additionalProperties : false ,
64+ } ) ,
65+ } ,
66+ } ,
67+ async function action ( ) {
68+ return API . v1 . success ( {
69+ accounts : await findWebdavAccountsByUserId ( { uid : this . userId } ) ,
70+ } ) ;
71+ } ,
72+ ) ;
1073
1174type POSTRemoveWebdavAccount = {
12- accountId : string ;
75+ accountId : IWebdavAccount [ '_id' ] ;
1376} ;
1477
1578const POSTRemoveWebdavAccountSchema = {
@@ -25,37 +88,96 @@ const POSTRemoveWebdavAccountSchema = {
2588
2689const isPOSTRemoveWebdavAccount = ajv . compile < POSTRemoveWebdavAccount > ( POSTRemoveWebdavAccountSchema ) ;
2790
28- API . v1 . addRoute (
29- 'webdav.getMyAccounts' ,
30- { authRequired : true } ,
31- {
32- async get ( ) {
33- return API . v1 . success ( {
34- accounts : await findWebdavAccountsByUserId ( { uid : this . userId } ) ,
35- } ) ;
36- } ,
37- } ,
38- ) ;
39-
40- API . v1 . addRoute (
91+ const webdavRemoveAccountEndpoints = API . v1 . post (
4192 'webdav.removeWebdavAccount' ,
4293 {
4394 authRequired : true ,
4495 validateParams : isPOSTRemoveWebdavAccount ,
45- } ,
46- {
47- async post ( ) {
48- const { accountId } = this . bodyParams ;
49-
50- const removed = await WebdavAccounts . removeByUserAndId ( accountId , this . userId ) ;
51- if ( removed ) {
52- void api . broadcast ( 'notify.webdav' , this . userId , {
53- type : 'removed' ,
54- account : { _id : accountId } ,
55- } ) ;
56- }
57-
58- return API . v1 . success ( { result : removed } ) ;
96+ body : isPOSTRemoveWebdavAccount ,
97+ response : {
98+ 200 : ajv . compile < {
99+ result : DeleteResult ;
100+ } > ( {
101+ type : 'object' ,
102+ properties : {
103+ result : {
104+ type : 'object' ,
105+ properties : {
106+ acknowledged : {
107+ type : 'boolean' ,
108+ } ,
109+ deletedCount : {
110+ type : 'integer' ,
111+ } ,
112+ } ,
113+ required : [ 'acknowledged' , 'deletedCount' ] ,
114+ additionalProperties : false ,
115+ } ,
116+ success : {
117+ type : 'boolean' ,
118+ description : 'Indicates if the request was successful.' ,
119+ } ,
120+ } ,
121+ required : [ 'result' , 'success' ] ,
122+ additionalProperties : false ,
123+ } ) ,
124+ 400 : ajv . compile ( {
125+ type : 'object' ,
126+ properties : {
127+ errorType : {
128+ type : 'string' ,
129+ } ,
130+ error : {
131+ type : 'string' ,
132+ } ,
133+ success : {
134+ type : 'boolean' ,
135+ description : 'Indicates if the request was successful.' ,
136+ } ,
137+ } ,
138+ required : [ 'success' , 'errorType' , 'error' ] ,
139+ additionalProperties : false ,
140+ } ) ,
141+ 401 : ajv . compile ( {
142+ type : 'object' ,
143+ properties : {
144+ message : {
145+ type : 'string' ,
146+ } ,
147+ success : {
148+ type : 'boolean' ,
149+ description : 'Indicates if the request was successful.' ,
150+ } ,
151+ } ,
152+ required : [ 'success' , 'message' ] ,
153+ additionalProperties : false ,
154+ } ) ,
59155 } ,
60156 } ,
157+ async function action ( ) {
158+ const { accountId } = this . bodyParams ;
159+
160+ const removed = await WebdavAccounts . removeByUserAndId ( accountId , this . userId ) ;
161+ if ( removed ) {
162+ void api . broadcast ( 'notify.webdav' , this . userId , {
163+ type : 'removed' ,
164+ account : { _id : accountId } ,
165+ } ) ;
166+ }
167+
168+ return API . v1 . success ( { result : removed } ) ;
169+ } ,
61170) ;
171+
172+ type WebdavGetMyAccountsEndpoints = ExtractRoutesFromAPI < typeof webdavGetMyAccountsEndpoints > ;
173+
174+ type WebdavRemoveAccountEndpoints = ExtractRoutesFromAPI < typeof webdavRemoveAccountEndpoints > ;
175+
176+ export type WebdavEndpoints = WebdavGetMyAccountsEndpoints | WebdavRemoveAccountEndpoints ;
177+
178+ declare module '@rocket.chat/rest-typings' {
179+ // eslint-disable-next-line @typescript-eslint/naming-convention, @typescript-eslint/no-empty-interface
180+ interface Endpoints extends WebdavGetMyAccountsEndpoints { }
181+ // eslint-disable-next-line @typescript-eslint/naming-convention, @typescript-eslint/no-empty-interface
182+ interface Endpoints extends WebdavRemoveAccountEndpoints { }
183+ }
0 commit comments