mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-21 15:20:34 +00:00
fix: validate request org id existance in guards
This commit is contained in:
@@ -41,6 +41,12 @@ export class EnsureTenantIsInitializedGuard implements CanActivate {
|
|||||||
}
|
}
|
||||||
const tenant = await this.tenancyContext.getTenant();
|
const tenant = await this.tenancyContext.getTenant();
|
||||||
|
|
||||||
|
if (!tenant) {
|
||||||
|
throw new UnauthorizedException({
|
||||||
|
message: 'Tenant not found.',
|
||||||
|
errors: [{ type: 'TENANT.NOT.FOUND' }],
|
||||||
|
});
|
||||||
|
}
|
||||||
if (!tenant?.initializedAt) {
|
if (!tenant?.initializedAt) {
|
||||||
throw new UnauthorizedException({
|
throw new UnauthorizedException({
|
||||||
statusCode: 400,
|
statusCode: 400,
|
||||||
|
|||||||
@@ -41,6 +41,12 @@ export class EnsureTenantIsSeededGuard implements CanActivate {
|
|||||||
}
|
}
|
||||||
const tenant = await this.tenancyContext.getTenant();
|
const tenant = await this.tenancyContext.getTenant();
|
||||||
|
|
||||||
|
if (!tenant) {
|
||||||
|
throw new UnauthorizedException({
|
||||||
|
message: 'Tenant not found.',
|
||||||
|
errors: [{ type: 'TENANT.NOT.FOUND' }],
|
||||||
|
});
|
||||||
|
}
|
||||||
if (!tenant.seededAt) {
|
if (!tenant.seededAt) {
|
||||||
throw new UnauthorizedException({
|
throw new UnauthorizedException({
|
||||||
message: 'Tenant database is not seeded with initial data yet.',
|
message: 'Tenant database is not seeded with initial data yet.',
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import { Inject, Injectable } from '@nestjs/common';
|
|||||||
import { ClsService } from 'nestjs-cls';
|
import { ClsService } from 'nestjs-cls';
|
||||||
import { SystemUser } from '../System/models/SystemUser';
|
import { SystemUser } from '../System/models/SystemUser';
|
||||||
import { TenantModel } from '../System/models/TenantModel';
|
import { TenantModel } from '../System/models/TenantModel';
|
||||||
|
import { ServiceError } from '../Items/ServiceError';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class TenancyContext {
|
export class TenancyContext {
|
||||||
@@ -20,7 +21,7 @@ export class TenancyContext {
|
|||||||
* @param {boolean} withMetadata - If true, the tenant metadata will be fetched.
|
* @param {boolean} withMetadata - If true, the tenant metadata will be fetched.
|
||||||
* @returns
|
* @returns
|
||||||
*/
|
*/
|
||||||
getTenant(withMetadata: boolean = false) {
|
async getTenant(withMetadata: boolean = false) {
|
||||||
// Get the tenant from the request headers.
|
// Get the tenant from the request headers.
|
||||||
const organizationId = this.cls.get('organizationId');
|
const organizationId = this.cls.get('organizationId');
|
||||||
|
|
||||||
@@ -32,7 +33,12 @@ export class TenancyContext {
|
|||||||
if (withMetadata) {
|
if (withMetadata) {
|
||||||
query.withGraphFetched('metadata');
|
query.withGraphFetched('metadata');
|
||||||
}
|
}
|
||||||
return query;
|
const queryResult = await query;
|
||||||
|
|
||||||
|
if (!queryResult) {
|
||||||
|
throw new ServiceError('TENANT_NOT_FOUND', 'Tenant not found');
|
||||||
|
}
|
||||||
|
return queryResult;
|
||||||
}
|
}
|
||||||
|
|
||||||
async getTenantMetadata() {
|
async getTenantMetadata() {
|
||||||
|
|||||||
Reference in New Issue
Block a user