Fix tests

This commit is contained in:
Romaric Mourgues
2023-04-14 10:30:07 +02:00
parent 18e10b6a8b
commit a46c6ca0a8
3 changed files with 20 additions and 72 deletions
@@ -1,13 +1,5 @@
import { Channel } from "../channels/entities";
import { Message } from "../messages/entities/messages";
import { Thread } from "../messages/entities/threads";
export type HookType = { export type HookType = {
type: "message"; type: string;
application_id: string; application_id: string;
company_id: string; company_id: string;
channel?: Channel;
thread: Thread;
message: Message;
}; };
@@ -1,21 +1,24 @@
import { FastifyInstance, FastifyReply, FastifyRequest, HTTPMethods } from "fastify"; import { FastifyInstance, FastifyReply, FastifyRequest, HTTPMethods } from "fastify";
import { ApplicationObject } from "../../../applications/entities/application"; import _ from "lodash";
import { import { v4 } from "uuid";
ApplicationApiExecutionContext,
ApplicationLoginRequest,
ApplicationLoginResponse,
ConfigureRequest,
} from "../types";
import { ResourceGetResponse } from "../../../../utils/types";
import { CrudException } from "../../../../core/platform/framework/api/crud-service"; import { CrudException } from "../../../../core/platform/framework/api/crud-service";
import { localEventBus } from "../../../../core/platform/framework/event-bus"; import { localEventBus } from "../../../../core/platform/framework/event-bus";
import { import {
RealtimeApplicationEvent, RealtimeApplicationEvent,
RealtimeBaseBusEvent, RealtimeBaseBusEvent,
} from "../../../../core/platform/services/realtime/types"; } from "../../../../core/platform/services/realtime/types";
import { ResourceGetResponse } from "../../../../utils/types";
import {
ApplicationObject,
getApplicationObject,
} from "../../../applications/entities/application";
import gr from "../../../global-resolver"; import gr from "../../../global-resolver";
import _ from "lodash"; import {
import { v4 } from "uuid"; ApplicationApiExecutionContext,
ApplicationLoginRequest,
ApplicationLoginResponse,
ConfigureRequest,
} from "../types";
export class ApplicationsApiController { export class ApplicationsApiController {
async token( async token(
@@ -27,12 +30,7 @@ export class ApplicationsApiController {
throw CrudException.forbidden("Application not found"); throw CrudException.forbidden("Application not found");
} }
const app = await gr.services.applications.marketplaceApps.get( const app = await gr.services.applications.marketplaceApps.get(request.body.id, context);
{
id: request.body.id,
},
context,
);
if (!app) { if (!app) {
throw CrudException.forbidden("Application not found"); throw CrudException.forbidden("Application not found");
@@ -60,16 +58,15 @@ export class ApplicationsApiController {
const context = getExecutionContext(request); const context = getExecutionContext(request);
const entity = await gr.services.applications.marketplaceApps.get( const entity = await gr.services.applications.marketplaceApps.get(
{ context.application_id,
id: context.application_id,
},
context, context,
); );
if (!entity) { if (!entity) {
throw CrudException.notFound("Application not found"); throw CrudException.notFound("Application not found");
} }
return { resource: entity.getApplicationObject() }; return { resource: getApplicationObject(entity) };
} }
async configure( async configure(
@@ -78,7 +75,7 @@ export class ApplicationsApiController {
): Promise<Record<string, string>> { ): Promise<Record<string, string>> {
const app_id = request.currentUser.application_id; const app_id = request.currentUser.application_id;
const context = getExecutionContext(request); const context = getExecutionContext(request);
const application = await gr.services.applications.marketplaceApps.get({ id: app_id }, context); const application = await gr.services.applications.marketplaceApps.get(app_id, context);
if (!application) { if (!application) {
throw CrudException.forbidden("Application not found"); throw CrudException.forbidden("Application not found");
@@ -125,9 +122,7 @@ export class ApplicationsApiController {
const context = getExecutionContext(request); const context = getExecutionContext(request);
const app = await gr.services.applications.marketplaceApps.get( const app = await gr.services.applications.marketplaceApps.get(
{ request.currentUser.application_id,
id: request.currentUser.application_id,
},
context, context,
); );
@@ -15,45 +15,6 @@ const routes: FastifyPluginCallback = (fastify: FastifyInstance, options, next)
const applicationController = new ApplicationController(); const applicationController = new ApplicationController();
const companyApplicationController = new CompanyApplicationController(); const companyApplicationController = new CompanyApplicationController();
const adminCheck = async (
request: FastifyRequest<{
Body: { resource: Application };
Params: { application_id: string };
}>,
) => {
try {
let companyId: string = request.body?.resource?.company_id;
if (request.params.application_id) {
const application = await gr.services.applications.marketplaceApps.get(
request.params.application_id,
undefined,
);
if (!application) {
throw fastify.httpErrors.notFound("Application is not defined");
}
companyId = application.company_id;
}
const userId = request.currentUser.id;
if (!companyId) {
throw fastify.httpErrors.forbidden(`Company ${companyId} not found`);
}
const companyUser = await checkUserBelongsToCompany(userId, companyId);
if (!hasCompanyAdminLevel(companyUser.role)) {
throw fastify.httpErrors.forbidden("You must be an admin of this company");
}
} catch (e) {
log.error(e);
throw e;
}
};
/** /**
* Applications collection * Applications collection
* Marketplace of applications * Marketplace of applications