Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/domain/innovation-hub/innovation.hub.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import { SpaceService } from '@domain/space/space/space.service';
import { AuthorizationPolicyService } from '@domain/common/authorization-policy/authorization.policy.service';
import { NamingService } from '@services/infrastructure/naming/naming.service';
import { TagsetReservedName } from '@common/enums/tagset.reserved.name';
import { IStorageAggregator } from '@domain/storage/storage-aggregator/storage.aggregator.interface';
import { SearchVisibility } from '@common/enums/search.visibility';
import { IAccount } from '@domain/space/account/account.interface';

@Injectable()
export class InnovationHubService {
Expand All @@ -31,7 +31,7 @@ export class InnovationHubService {

public async createInnovationHub(
createData: CreateInnovationHubInput,
storageAggregator: IStorageAggregator
account: IAccount
): Promise<IInnovationHub | never> {
try {
await this.validateCreateInput(createData);
Expand All @@ -43,6 +43,13 @@ export class InnovationHubService {
);
}

if (!account.storageAggregator) {
throw new EntityNotFoundException(
`Unable to load storage aggregator on account for creating innovation Hub: ${account.id}`,
LogContext.ACCOUNT
);
}

const subdomainAvailable =
await this.namingService.isInnovationHubSubdomainAvailable(
createData.subdomain
Expand Down Expand Up @@ -73,11 +80,12 @@ export class InnovationHubService {
hub.authorization = new AuthorizationPolicy();
hub.listedInStore = true;
hub.searchVisibility = SearchVisibility.ACCOUNT;
hub.account = account;

hub.profile = await this.profileService.createProfile(
createData.profileData,
ProfileType.INNOVATION_HUB,
storageAggregator
account.storageAggregator
);

await this.profileService.addTagsetOnProfile(hub.profile, {
Expand Down
13 changes: 2 additions & 11 deletions src/domain/space/account/account.resolver.mutations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@ import { CreateAccountInput } from './dto';
import { RelationshipNotFoundException } from '@common/exceptions/relationship.not.found.exception';
import { LogContext } from '@common/enums/logging.context';
import { SpaceLevel } from '@common/enums/space.level';
import {
EntityNotFoundException,
EntityNotInitializedException,
} from '@common/exceptions';
import { EntityNotInitializedException } from '@common/exceptions';
import { IVirtualContributor } from '@domain/community/virtual-contributor/virtual.contributor.interface';
import { CreateVirtualContributorOnAccountInput } from './dto/account.dto.create.virtual.contributor';
import { VirtualContributorAuthorizationService } from '@domain/community/virtual-contributor/virtual.contributor.service.authorization';
Expand Down Expand Up @@ -313,15 +310,9 @@ export class AccountResolverMutations {
}
);

if (!account.storageAggregator) {
throw new EntityNotFoundException(
`Unable to load storage aggregator on account for creating innovation Hub: ${account.id}`,
LogContext.ACCOUNT
);
}
let innovationHub = await this.innovationHubService.createInnovationHub(
createData,
account.storageAggregator
account
);
innovationHub =
await this.innovationHubAuthorizationService.applyAuthorizationPolicyAndSave(
Expand Down
2 changes: 1 addition & 1 deletion src/domain/space/account/account.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ export class AccountService {
}
const hub = await this.innovationHubService.createInnovationHub(
innovationHubData,
account.storageAggregator
account
);
hub.account = account;
return await this.innovationHubService.save(hub);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import { StorageAggregatorNotFoundException } from '@common/exceptions/storage.a
import { WINSTON_MODULE_NEST_PROVIDER } from 'nest-winston';
import { Space } from '@domain/space/space/space.entity';
import { SpaceLevel } from '@common/enums/space.level';
import { isUUID } from 'class-validator';
import { InvalidUUID } from '@common/exceptions/invalid.uuid';

@Injectable()
export class StorageAggregatorResolverService {
Expand Down Expand Up @@ -60,15 +62,6 @@ export class StorageAggregatorResolverService {
return this.getStorageAggregatorOrFail(result.storageAggregatorId);
}

public async getLibraryStorageAggregator(): Promise<IStorageAggregator> {
const query = `SELECT \`storageAggregatorId\`
FROM \`library\` LIMIT 1`;
const [result]: {
storageAggregatorId: string;
}[] = await this.entityManager.connection.query(query);
return this.getStorageAggregatorOrFail(result.storageAggregatorId);
}

public async getParentEntityInformation(
storageAggregatorID: string
): Promise<{
Expand Down Expand Up @@ -105,29 +98,39 @@ export class StorageAggregatorResolverService {
public async getStorageAggregatorForTemplatesSet(
templatesSetId: string
): Promise<IStorageAggregator> {
const space = await this.entityManager.findOne(Space, {
where: {
account: {
library: {
id: templatesSetId,
},
},
},
relations: {
storageAggregator: true,
},
});

if (space && space.storageAggregator) {
return await this.getStorageAggregatorOrFail(space.storageAggregator.id);
// This query is a bit tricky because we have a TemplateSetId and we need to find the StorageAggregator
// associated to it's parent.
// TemplatesSets can be in a Space (the space's templates), or an InnovationPack (the templates of that IP).
// In practice it's just a ManyToMany relationship between Spaces/IPs and the templates associated to them.

// The parent of Spaces and IPs is an Account, Spaces have a StorageAggregator but Account also has a StorageAggregator.
// So for templatesSets in spaces we return the Space's StoreAggregator, and for templatesSets in IPs we return the Account's StorageAggregator.

if (!isUUID(templatesSetId)) {
throw new InvalidUUID(
'Invalid UUID provided to find the StorageAggregator of a templateSet',
LogContext.COMMUNITY,
{ provided: templatesSetId }
);
}

const query = `SELECT \`id\` FROM \`innovation_pack\`
WHERE \`innovation_pack\`.\`templatesSetId\`='${templatesSetId}'`;
// We are doing this UNION here, but only one of them will return a result.
const query = `
SELECT account.storageAggregatorId FROM account
WHERE account.libraryId = '${templatesSetId}'
UNION
SELECT account.storageAggregatorId FROM innovation_pack
JOIN account ON innovation_pack.accountId = account.id
WHERE innovation_pack.templatesSetId = '${templatesSetId}'`;

// If we want to get the storageAggregator of the space in the first case, we would do:
// SELECT space.storageAggregatorId FROM space
// JOIN account ON space.accountId = account.id
// WHERE space.level = 0 AND account.libraryId = '${templatesSetId}'

const [result] = await this.entityManager.connection.query(query);
if (result) {
// use the library sorage aggregator
return await this.getLibraryStorageAggregator();
return this.getStorageAggregatorOrFail(result.storageAggregatorId);
}

throw new StorageAggregatorNotFoundException(
Expand All @@ -147,9 +150,8 @@ export class StorageAggregatorResolverService {
public async getStorageAggregatorForCalendar(
calendarID: string
): Promise<IStorageAggregator> {
const storageAggregatorId = await this.getStorageAggregatorIdForCalendar(
calendarID
);
const storageAggregatorId =
await this.getStorageAggregatorIdForCalendar(calendarID);
return await this.getStorageAggregatorOrFail(storageAggregatorId);
}

Expand Down Expand Up @@ -192,9 +194,8 @@ export class StorageAggregatorResolverService {
public async getStorageAggregatorForCommunity(
communityID: string
): Promise<IStorageAggregator> {
const storageAggregatorId = await this.getStorageAggregatorIdForCommunity(
communityID
);
const storageAggregatorId =
await this.getStorageAggregatorIdForCommunity(communityID);
return await this.getStorageAggregatorOrFail(storageAggregatorId);
}

Expand Down Expand Up @@ -223,9 +224,8 @@ export class StorageAggregatorResolverService {
public async getStorageAggregatorForCallout(
calloutID: string
): Promise<IStorageAggregator> {
const storageAggregatorId = await this.getStorageAggregatorIdForCallout(
calloutID
);
const storageAggregatorId =
await this.getStorageAggregatorIdForCallout(calloutID);
return await this.getStorageAggregatorOrFail(storageAggregatorId);
}

Expand Down