fix pluggin bug

This commit is contained in:
2026-09-09 17:04:33 +03:30
parent 58744d0f91
commit 08f648457f
3 changed files with 142 additions and 24 deletions
+52 -23
View File
@@ -7,6 +7,7 @@ import {
NotFoundException,
OnModuleDestroy,
OnModuleInit,
ServiceUnavailableException,
} from '@nestjs/common';
import {
PluginCatalogItem,
@@ -271,14 +272,14 @@ export class PluginStoreService implements OnModuleInit, OnModuleDestroy {
create: {
catalogItemId: id,
durationDays: price.durationDays,
enabled: price.enabled,
originalPriceRials: price.originalPriceRials,
finalPriceRials: price.finalPriceRials,
enabled: Boolean(price.enabled),
originalPriceRials: Math.trunc(Number(price.originalPriceRials) || 0),
finalPriceRials: Math.trunc(Number(price.finalPriceRials) || 0),
},
update: {
enabled: price.enabled,
originalPriceRials: price.originalPriceRials,
finalPriceRials: price.finalPriceRials,
enabled: Boolean(price.enabled),
originalPriceRials: Math.trunc(Number(price.originalPriceRials) || 0),
finalPriceRials: Math.trunc(Number(price.finalPriceRials) || 0),
},
});
}
@@ -491,33 +492,53 @@ export class PluginStoreService implements OnModuleInit, OnModuleDestroy {
return await this.buildRemoteStore(tenantId);
} catch (error: any) {
if (error instanceof ForbiddenException) throw error;
this.logger.warn(`remoteStore skipped: ${error?.message || error}`);
return { plugin_store_enabled: true, items: [] };
this.logger.warn(`remoteStore failed: ${error?.message || error}`);
throw new ServiceUnavailableException(
error?.message || 'فروشگاه افزونه در سوپرادمین در دسترس نیست',
);
}
}
private isCatalogPriceEnabled(price: Pick<PluginCatalogPrice, 'enabled'> | { enabled?: unknown }) {
const flag = (price as { enabled?: unknown }).enabled;
return flag === true || flag === 1 || flag === 'true' || flag === 't';
}
private async buildRemoteStore(tenantId: string) {
await this.seedCatalog();
const items = await this.prisma.pluginCatalogItem.findMany({
where: { inStore: true },
orderBy: { titleFa: 'asc' },
include: { prices: { orderBy: { durationDays: 'asc' } } },
});
const subs = await this.prisma.pluginSubscription.findMany({
where: { tenantId, settingKey: { not: null } },
});
const pending = await this.prisma.pluginPurchaseRequest.findMany({
where: { tenantId, status: 'pending' },
select: { catalogItemId: true },
});
let subs: Array<{
settingKey: string | null;
expiresAt: Date | null;
isActive: boolean;
}> = [];
let pending: Array<{ catalogItemId: string }> = [];
try {
subs = await this.prisma.pluginSubscription.findMany({
where: { tenantId, settingKey: { not: null } },
select: { settingKey: true, expiresAt: true, isActive: true },
});
} catch (error: any) {
this.logger.warn(`remoteStore subs skipped: ${error?.message || error}`);
}
try {
pending = await this.prisma.pluginPurchaseRequest.findMany({
where: { tenantId, status: 'pending' },
select: { catalogItemId: true },
});
} catch (error: any) {
this.logger.warn(`remoteStore pending skipped: ${error?.message || error}`);
}
const pendingIds = new Set(pending.map((p) => p.catalogItemId));
const now = Date.now();
return {
plugin_store_enabled: true,
items: items
.map((item) => {
const enabledPrices = item.prices.filter((p) => p.enabled);
if (!enabledPrices.length) return null;
const mapped = items
.map((item) => {
const enabledPrices = item.prices.filter((p) => this.isCatalogPriceEnabled(p));
if (!enabledPrices.length) return null;
const sub = subs
.filter((s) => s.settingKey === item.settingKey)
.sort((a, b) => (b.expiresAt?.getTime() || 0) - (a.expiresAt?.getTime() || 0))[0];
@@ -553,7 +574,15 @@ export class PluginStoreService implements OnModuleInit, OnModuleDestroy {
pendingRequest: hasPending,
};
})
.filter(Boolean),
.filter(Boolean);
this.logger.log(
`remoteStore tenant=${tenantId} catalog=${items.length} listed=${mapped.length}`,
);
return {
plugin_store_enabled: true,
items: mapped,
};
}