fix some extention bug

This commit is contained in:
2026-09-16 13:44:58 +03:30
parent cf92635c0e
commit 6ba6a9cd45
3 changed files with 43 additions and 22 deletions
+4 -18
View File
@@ -1,6 +1,6 @@
/** Shared Iran timezone helpers for SuperAdmin API. */ /** Shared Iran timezone helpers for SuperAdmin API. */
import { toGregorian, jalaaliMonthLength } from 'jalaali-js'; import { toGregorian, jalaaliMonthLength, toJalaali } from 'jalaali-js';
export const IRAN_TZ = 'Asia/Tehran'; export const IRAN_TZ = 'Asia/Tehran';
@@ -45,23 +45,9 @@ export type IranJalaliYmd = { year: number; month: number; day: number };
/** Jalali Y/M/D for a Date in Asia/Tehran. */ /** Jalali Y/M/D for a Date in Asia/Tehran. */
export function iranJalaliParts(date: Date): IranJalaliYmd { export function iranJalaliParts(date: Date): IranJalaliYmd {
const parts = new Intl.DateTimeFormat('en-u-ca-persian', { const [gy, gm, gd] = iranYmd(date).split('-').map(Number);
timeZone: IRAN_TZ, const { jy, jm, jd } = toJalaali(gy, gm, gd);
year: 'numeric', return { year: jy, month: jm, day: jd };
month: 'numeric',
day: 'numeric',
}).formatToParts(date);
const num = (type: Intl.DateTimeFormatPartTypes) => {
const raw = parts.find((p) => p.type === type)?.value ?? '';
const n = Number(raw);
if (!Number.isFinite(n)) {
throw new Error(`Cannot read Jalali ${type} from ${date.toISOString()}`);
}
return n;
};
return { year: num('year'), month: num('month'), day: num('day') };
} }
/** Noon Iran (+03:30) for a Jalali calendar day. */ /** Noon Iran (+03:30) for a Jalali calendar day. */
+12
View File
@@ -40,6 +40,10 @@ export class CreatePluginDto {
@IsISO8601() @IsISO8601()
chargedAt?: string; chargedAt?: string;
@IsOptional()
@IsISO8601()
expiresAt?: string;
@IsOptional() @IsOptional()
@Type(() => Number) @Type(() => Number)
@IsInt() @IsInt()
@@ -90,6 +94,10 @@ export class UpdatePluginDto {
@IsISO8601() @IsISO8601()
chargedAt?: string | null; chargedAt?: string | null;
@IsOptional()
@IsISO8601()
expiresAt?: string | null;
@IsOptional() @IsOptional()
@Type(() => Number) @Type(() => Number)
@IsInt() @IsInt()
@@ -122,6 +130,10 @@ export class RenewPluginDto {
@IsISO8601() @IsISO8601()
chargedAt?: string; chargedAt?: string;
@IsOptional()
@IsISO8601()
expiresAt?: string;
@Type(() => Number) @Type(() => Number)
@IsInt() @IsInt()
@IsIn(PLUGIN_PLAN_MONTHS as unknown as number[]) @IsIn(PLUGIN_PLAN_MONTHS as unknown as number[])
+26 -3
View File
@@ -248,12 +248,21 @@ export class PluginsService implements OnModuleInit, OnModuleDestroy {
let planMonths = dto.planMonths ?? null; let planMonths = dto.planMonths ?? null;
let expiresAt: Date | null = null; let expiresAt: Date | null = null;
if (dto.expiresAt) {
expiresAt = new Date(dto.expiresAt);
if (Number.isNaN(expiresAt.getTime())) {
throw new BadRequestException('تاریخ انقضا نامعتبر است');
}
}
if (chargedAt && planMonths) { if (chargedAt && planMonths) {
if (!PLUGIN_PLAN_MONTHS.includes(planMonths as any)) { if (!PLUGIN_PLAN_MONTHS.includes(planMonths as any)) {
throw new BadRequestException('مدت اشتراک نامعتبر است'); throw new BadRequestException('مدت اشتراک نامعتبر است');
} }
if (!expiresAt) {
expiresAt = addIranPlanExpiry(chargedAt, planMonths); expiresAt = addIranPlanExpiry(chargedAt, planMonths);
} }
}
const settingKey = const settingKey =
tenant.product === 'khanegi' tenant.product === 'khanegi'
@@ -330,7 +339,16 @@ export class PluginsService implements OnModuleInit, OnModuleDestroy {
if (dto.planMonths !== undefined) { if (dto.planMonths !== undefined) {
planMonths = dto.planMonths; planMonths = dto.planMonths;
} }
if ( if (dto.expiresAt !== undefined) {
if (dto.expiresAt) {
expiresAt = new Date(dto.expiresAt);
if (Number.isNaN(expiresAt.getTime())) {
throw new BadRequestException('تاریخ انقضا نامعتبر است');
}
} else {
expiresAt = null;
}
} else if (
(dto.chargedAt !== undefined || dto.planMonths !== undefined) && (dto.chargedAt !== undefined || dto.planMonths !== undefined) &&
chargedAt && chargedAt &&
planMonths planMonths
@@ -358,7 +376,9 @@ export class PluginsService implements OnModuleInit, OnModuleDestroy {
...(dto.priceRials !== undefined && { priceRials: dto.priceRials }), ...(dto.priceRials !== undefined && { priceRials: dto.priceRials }),
...(dto.chargedAt !== undefined && { chargedAt }), ...(dto.chargedAt !== undefined && { chargedAt }),
...(dto.planMonths !== undefined && { planMonths }), ...(dto.planMonths !== undefined && { planMonths }),
...(dto.chargedAt !== undefined || dto.planMonths !== undefined ...(dto.chargedAt !== undefined ||
dto.planMonths !== undefined ||
dto.expiresAt !== undefined
? { expiresAt } ? { expiresAt }
: {}), : {}),
...(dto.alertDaysBefore !== undefined && { ...(dto.alertDaysBefore !== undefined && {
@@ -406,7 +426,10 @@ export class PluginsService implements OnModuleInit, OnModuleDestroy {
if (Number.isNaN(chargedAt.getTime())) { if (Number.isNaN(chargedAt.getTime())) {
throw new BadRequestException('تاریخ شارژ نامعتبر است'); throw new BadRequestException('تاریخ شارژ نامعتبر است');
} }
const expiresAt = addIranPlanExpiry(chargedAt, dto.planMonths); let expiresAt = dto.expiresAt ? new Date(dto.expiresAt) : addIranPlanExpiry(chargedAt, dto.planMonths);
if (Number.isNaN(expiresAt.getTime())) {
throw new BadRequestException('تاریخ انقضا نامعتبر است');
}
await this.prisma.$transaction([ await this.prisma.$transaction([
this.prisma.pluginSubscription.update({ this.prisma.pluginSubscription.update({