Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | import { Injectable, Logger, BadRequestException, NotFoundException, } from '@nestjs/common'; import { InjectModel } from '@nestjs/mongoose'; import { Model } from 'mongoose'; import { MarketplaceApp, MarketplaceInstallation, } from './schemas/marketplace.schema'; @Injectable() export class MarketplaceService { private readonly logger = new Logger(MarketplaceService.name); constructor( @InjectModel(MarketplaceApp.name) private readonly appModel: Model<MarketplaceApp>, @InjectModel(MarketplaceInstallation.name) private readonly installationModel: Model<MarketplaceInstallation>, ) {} async registerApp(params: { appKey: string; name: string; description: string; requiredScopes: string[]; }): Promise<MarketplaceApp> { const existing = await this.appModel .findOne({ appKey: params.appKey }) .exec(); const doc = existing ?? new this.appModel({ appKey: params.appKey }); doc.name = params.name; doc.description = params.description; doc.requiredScopes = params.requiredScopes; doc.isActive = true; await doc.save(); return doc; } async installApp( tenantId: string, appKey: string, settings: Record<string, any> = {}, installedBy?: string, ): Promise<MarketplaceInstallation> { const app = await this.appModel.findOne({ appKey, isActive: true }).exec(); if (!app) { throw new NotFoundException('App not found or is currently inactive'); } const installation = await this.installationModel .findOneAndUpdate( { tenantId, appKey }, { status: 'installed', settings, installedBy: installedBy || 'system' }, { upsert: true, new: true }, ) .exec(); this.logger.log( `App ${appKey} successfully installed for tenant ${tenantId}`, ); return installation; } async uninstallApp(tenantId: string, appKey: string): Promise<void> { const result = await this.installationModel .findOneAndUpdate( { tenantId, appKey, status: 'installed' }, { status: 'uninstalled' }, ) .exec(); if (!result) { throw new BadRequestException( 'App is not currently installed or already uninstalled', ); } } async getInstalledApps(tenantId: string): Promise<MarketplaceInstallation[]> { return this.installationModel .find({ tenantId, status: 'installed' }) .exec(); } } |