import type { IOAuthApps } from '@rocket.chat/core-typings'; import type { ServerMethods } from '@rocket.chat/ddp-client'; import { OAuthAccessTokens, OAuthApps, OAuthAuthCodes } from '@rocket.chat/models'; import { Meteor } from 'meteor/meteor'; import { hasPermissionAsync } from '../../lib/authorization/hasPermission'; import { methodDeprecationLogger } from '../../lib/deprecationWarningLogger'; declare module '@rocket.chat/ddp-client' { // eslint-disable-next-line @typescript-eslint/naming-convention interface ServerMethods { deleteOAuthApp(applicationId: IOAuthApps['_id']): boolean; } } export const deleteOAuthApp = async (userId: string, applicationId: IOAuthApps['_id']): Promise => { if (!(await hasPermissionAsync(userId, 'manage-oauth-apps'))) { throw new Meteor.Error('error-not-allowed', 'Not allowed', { method: 'deleteOAuthApp' }); } const application = await OAuthApps.findOneAndDeleteById(applicationId, { projection: { clientId: 1 } }); if (!application) { throw new Meteor.Error('error-application-not-found', 'Application not found', { method: 'deleteOAuthApp', }); } await OAuthAccessTokens.deleteMany({ clientId: application.clientId }); await OAuthAuthCodes.deleteMany({ clientId: application.clientId }); return true; }; Meteor.methods({ async deleteOAuthApp(applicationId) { methodDeprecationLogger.method('deleteOAuthApp', '9.0.0', '/v1/oauth-apps.delete'); if (!this.userId) { throw new Meteor.Error('error-invalid-user', 'Invalid user', { method: 'deleteOAuthApp' }); } return deleteOAuthApp(this.userId, applicationId); }, });