Refactor SIM management and usage services for improved scheduling and error handling
- Updated SimManagementService to automatically schedule plan changes for the 1st of the next month instead of immediate execution. - Enhanced SimUsageStoreService by introducing a private store getter for safer access to the SIM usage data. - Improved error handling in upsert and find methods to ensure proper logging and handling of unconfigured stores. - Refined port management logic in manage.sh to ensure desired ports are free and provide clearer error messages for port conflicts.
This commit is contained in:
parent
6e27d8a21e
commit
340ff94d07
@ -580,15 +580,18 @@ export class SimManagementService {
|
|||||||
throw new BadRequestException("Invalid plan code");
|
throw new BadRequestException("Invalid plan code");
|
||||||
}
|
}
|
||||||
|
|
||||||
// TESTING: schedule for immediate execution (today)
|
// Automatically set to 1st of next month
|
||||||
const now = new Date();
|
const nextMonth = new Date();
|
||||||
|
nextMonth.setMonth(nextMonth.getMonth() + 1);
|
||||||
|
nextMonth.setDate(1); // Set to 1st of the month
|
||||||
|
|
||||||
// Format as YYYYMMDD for Freebit API
|
// Format as YYYYMMDD for Freebit API
|
||||||
const year = now.getFullYear();
|
const year = nextMonth.getFullYear();
|
||||||
const month = String(now.getMonth() + 1).padStart(2, "0");
|
const month = String(nextMonth.getMonth() + 1).padStart(2, "0");
|
||||||
const day = String(now.getDate()).padStart(2, "0");
|
const day = String(nextMonth.getDate()).padStart(2, "0");
|
||||||
const scheduledAt = `${year}${month}${day}`;
|
const scheduledAt = `${year}${month}${day}`;
|
||||||
|
|
||||||
this.logger.log(`Scheduled plan change for testing (immediate): ${scheduledAt}`, {
|
this.logger.log(`Auto-scheduled plan change to 1st of next month: ${scheduledAt}`, {
|
||||||
userId,
|
userId,
|
||||||
subscriptionId,
|
subscriptionId,
|
||||||
account,
|
account,
|
||||||
|
|||||||
@ -9,6 +9,11 @@ export class SimUsageStoreService {
|
|||||||
@Inject(Logger) private readonly logger: Logger
|
@Inject(Logger) private readonly logger: Logger
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
|
private get store(): any | null {
|
||||||
|
const s = (this.prisma as any)?.simUsageDaily;
|
||||||
|
return s && typeof s === 'object' ? s : null;
|
||||||
|
}
|
||||||
|
|
||||||
private normalizeDate(date?: Date): Date {
|
private normalizeDate(date?: Date): Date {
|
||||||
const d = date ? new Date(date) : new Date();
|
const d = date ? new Date(date) : new Date();
|
||||||
// strip time to YYYY-MM-DD
|
// strip time to YYYY-MM-DD
|
||||||
@ -19,7 +24,12 @@ export class SimUsageStoreService {
|
|||||||
async upsertToday(account: string, usageMb: number, date?: Date): Promise<void> {
|
async upsertToday(account: string, usageMb: number, date?: Date): Promise<void> {
|
||||||
const day = this.normalizeDate(date);
|
const day = this.normalizeDate(date);
|
||||||
try {
|
try {
|
||||||
await (this.prisma as any).simUsageDaily.upsert({
|
const store = this.store;
|
||||||
|
if (!store) {
|
||||||
|
this.logger.debug('SIM usage store not configured; skipping persist');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
await store.upsert({
|
||||||
where: { account_date: { account, date: day } },
|
where: { account_date: { account, date: day } },
|
||||||
update: { usageMb },
|
update: { usageMb },
|
||||||
create: { account, date: day, usageMb },
|
create: { account, date: day, usageMb },
|
||||||
@ -37,7 +47,9 @@ export class SimUsageStoreService {
|
|||||||
const end = this.normalizeDate();
|
const end = this.normalizeDate();
|
||||||
const start = new Date(end);
|
const start = new Date(end);
|
||||||
start.setUTCDate(end.getUTCDate() - (days - 1));
|
start.setUTCDate(end.getUTCDate() - (days - 1));
|
||||||
const rows = (await (this.prisma as any).simUsageDaily.findMany({
|
const store = this.store;
|
||||||
|
if (!store) return [];
|
||||||
|
const rows = (await store.findMany({
|
||||||
where: { account, date: { gte: start, lte: end } },
|
where: { account, date: { gte: start, lte: end } },
|
||||||
orderBy: { date: "desc" },
|
orderBy: { date: "desc" },
|
||||||
})) as Array<{ date: Date; usageMb: number }>;
|
})) as Array<{ date: Date; usageMb: number }>;
|
||||||
@ -47,7 +59,9 @@ export class SimUsageStoreService {
|
|||||||
async cleanupPreviousMonths(): Promise<number> {
|
async cleanupPreviousMonths(): Promise<number> {
|
||||||
const now = new Date();
|
const now = new Date();
|
||||||
const firstOfMonth = new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), 1));
|
const firstOfMonth = new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), 1));
|
||||||
const result = (await (this.prisma as any).simUsageDaily.deleteMany({
|
const store = this.store;
|
||||||
|
if (!store) return 0;
|
||||||
|
const result = (await store.deleteMany({
|
||||||
where: { date: { lt: firstOfMonth } },
|
where: { date: { lt: firstOfMonth } },
|
||||||
})) as { count: number };
|
})) as { count: number };
|
||||||
return result.count;
|
return result.count;
|
||||||
|
|||||||
@ -215,20 +215,31 @@ start_services() {
|
|||||||
|
|
||||||
local next="${NEXT_PORT:-$NEXT_PORT_DEFAULT}"
|
local next="${NEXT_PORT:-$NEXT_PORT_DEFAULT}"
|
||||||
local bff="${BFF_PORT:-$BFF_PORT_DEFAULT}"
|
local bff="${BFF_PORT:-$BFF_PORT_DEFAULT}"
|
||||||
# Auto-pick free ports if occupied
|
# Ensure desired ports are free; kill any listeners
|
||||||
|
kill_by_port "$next"
|
||||||
|
kill_by_port "$bff"
|
||||||
|
# If still busy, either auto-shift (if allowed) or fail
|
||||||
|
if ! is_port_free "$next"; then
|
||||||
|
if [ "${ALLOW_PORT_SHIFT:-0}" = "1" ]; then
|
||||||
local next_free
|
local next_free
|
||||||
next_free="$(find_free_port "$next")"
|
next_free="$(find_free_port "$next")"
|
||||||
if [ "$next_free" != "$next" ]; then
|
|
||||||
warn "Port $next in use; assigning NEXT_PORT=$next_free"
|
warn "Port $next in use; assigning NEXT_PORT=$next_free"
|
||||||
export NEXT_PORT="$next_free"
|
export NEXT_PORT="$next_free"
|
||||||
next="$next_free"
|
next="$next_free"
|
||||||
|
else
|
||||||
|
fail "Port $next is in use. Stop the process or run '$0 cleanup'. Set ALLOW_PORT_SHIFT=1 to auto-assign another port."
|
||||||
fi
|
fi
|
||||||
|
fi
|
||||||
|
if ! is_port_free "$bff"; then
|
||||||
|
if [ "${ALLOW_PORT_SHIFT:-0}" = "1" ]; then
|
||||||
local bff_free
|
local bff_free
|
||||||
bff_free="$(find_free_port "$bff")"
|
bff_free="$(find_free_port "$bff")"
|
||||||
if [ "$bff_free" != "$bff" ]; then
|
|
||||||
warn "Port $bff in use; assigning BFF_PORT=$bff_free"
|
warn "Port $bff in use; assigning BFF_PORT=$bff_free"
|
||||||
export BFF_PORT="$bff_free"
|
export BFF_PORT="$bff_free"
|
||||||
bff="$bff_free"
|
bff="$bff_free"
|
||||||
|
else
|
||||||
|
fail "Port $bff is in use. Stop the process or run '$0 cleanup'. Set ALLOW_PORT_SHIFT=1 to auto-assign another port."
|
||||||
|
fi
|
||||||
fi
|
fi
|
||||||
log "✅ Development services are running!"
|
log "✅ Development services are running!"
|
||||||
log "🔗 Database: postgresql://${POSTGRES_USER:-$DB_USER_DEFAULT}:${POSTGRES_PASSWORD:-${POSTGRES_PASSWORD:-dev}}@localhost:5432/${POSTGRES_DB:-$DB_NAME_DEFAULT}"
|
log "🔗 Database: postgresql://${POSTGRES_USER:-$DB_USER_DEFAULT}:${POSTGRES_PASSWORD:-${POSTGRES_PASSWORD:-dev}}@localhost:5432/${POSTGRES_DB:-$DB_NAME_DEFAULT}"
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user