Fix landing 500 from invalid Asia/Esfahan timezone.

Normalize Iran city aliases to Asia/Tehran, harden the clock, and make settings timezone a safe select.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-09-30 21:25:45 +03:30
co-authored by Cursor
parent 183ebf5e7f
commit e9eafdaabc
7 changed files with 98 additions and 12 deletions
+5 -1
View File
@@ -25,7 +25,11 @@
</div> </div>
<div class="field"> <div class="field">
<label class="label">منطقه زمانی</label> <label class="label">منطقه زمانی</label>
<input v-model="form.timezone" class="input" dir="ltr" placeholder="Asia/Tehran" /> <select v-model="form.timezone" class="input" dir="ltr">
<option value="Asia/Tehran">Asia/Tehran (ایران — تهران، اصفهان، مشهد و …)</option>
<option value="UTC">UTC</option>
</select>
<p class="muted" style="margin-top: 0.35rem">برای همهٔ شهرهای ایران فقط Asia/Tehran معتبر است.</p>
</div> </div>
<div class="field" style="grid-column: 1 / -1"> <div class="field" style="grid-column: 1 / -1">
<label class="label">آدرس لوگو</label> <label class="label">آدرس لوگو</label>
+15 -2
View File
@@ -1,4 +1,5 @@
import { Injectable, NotFoundException } from "@nestjs/common"; import { Injectable, NotFoundException } from "@nestjs/common";
import { resolveTimezone } from "@montech/shared";
import { PrismaService } from "../prisma/prisma.service"; import { PrismaService } from "../prisma/prisma.service";
@Injectable() @Injectable()
@@ -19,11 +20,12 @@ export class ContentService {
socials?: object; socials?: object;
logoUrl?: string | null; logoUrl?: string | null;
}) { }) {
const { logoUrl, ...rest } = data; const { logoUrl, timezone, ...rest } = data;
return this.prisma.siteSettings.update({ return this.prisma.siteSettings.update({
where: { id: "default" }, where: { id: "default" },
data: { data: {
...rest, ...rest,
...(timezone !== undefined ? { timezone: resolveTimezone(timezone) } : {}),
...(logoUrl !== undefined ? { logoUrl: logoUrl || null } : {}), ...(logoUrl !== undefined ? { logoUrl: logoUrl || null } : {}),
}, },
}); });
@@ -48,11 +50,22 @@ export class ContentService {
} }
async getPublicSite() { async getPublicSite() {
const [settings, sections, pages] = await Promise.all([ const [settingsRaw, sections, pages] = await Promise.all([
this.getSettings(), this.getSettings(),
this.listSections(), this.listSections(),
this.prisma.page.findMany({ include: { seo: true } }), this.prisma.page.findMany({ include: { seo: true } }),
]); ]);
const settings = {
...settingsRaw,
timezone: resolveTimezone(settingsRaw.timezone),
};
// heal invalid timezone stored in DB (e.g. Asia/Esfahan)
if (settingsRaw.timezone !== settings.timezone) {
await this.prisma.siteSettings.update({
where: { id: "default" },
data: { timezone: settings.timezone },
});
}
const sectionMap: Record<string, unknown> = {}; const sectionMap: Record<string, unknown> = {};
for (const s of sections) sectionMap[s.key] = s.data; for (const s of sections) sectionMap[s.key] = s.data;
return { settings, sections: sectionMap, pages }; return { settings, sections: sectionMap, pages };
+7 -8
View File
@@ -18,20 +18,19 @@
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import { formatClock } from "~/utils/timezone";
const props = defineProps<{ brand?: string; timezone?: string }>(); const props = defineProps<{ brand?: string; timezone?: string }>();
const brand = computed(() => props.brand || "منتک"); const brand = computed(() => props.brand || "منتک");
const time = ref(""); const time = ref("");
onMounted(() => { onMounted(() => {
const tz = props.timezone || "Asia/Tehran";
const tick = () => { const tick = () => {
time.value = new Intl.DateTimeFormat("fa-IR", { try {
timeZone: tz, time.value = formatClock(new Date(), props.timezone);
hour: "2-digit", } catch {
minute: "2-digit", time.value = "";
second: "2-digit", }
hour12: false,
}).format(new Date());
}; };
tick(); tick();
const id = setInterval(tick, 1000); const id = setInterval(tick, 1000);
+4 -1
View File
@@ -29,7 +29,10 @@
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
const { data: site } = await useAsyncData("public-site", () => fetchPublicSite()); const { data: site } = await useAsyncData("public-site", () => fetchPublicSite(), {
// always refetch so admin edits show up after refresh
getCachedData: () => undefined,
});
const homePage = computed(() => site.value?.pages?.find((p: any) => p.key === "home")); const homePage = computed(() => site.value?.pages?.find((p: any) => p.key === "home"));
applyPageSeo(homePage.value?.seo); applyPageSeo(homePage.value?.seo);
</script> </script>
+32
View File
@@ -0,0 +1,32 @@
const FALLBACK_TZ = "Asia/Tehran";
const TZ_ALIASES: Record<string, string> = {
"Asia/Esfahan": FALLBACK_TZ,
"Asia/Isfahan": FALLBACK_TZ,
"Asia/Mashhad": FALLBACK_TZ,
"Asia/Shiraz": FALLBACK_TZ,
"Asia/Tabriz": FALLBACK_TZ,
Iran: FALLBACK_TZ,
};
export function resolveTimezone(input?: string | null): string {
const raw = (input || FALLBACK_TZ).trim() || FALLBACK_TZ;
const candidate = TZ_ALIASES[raw] || raw;
try {
Intl.DateTimeFormat("en-US", { timeZone: candidate });
return candidate;
} catch {
return FALLBACK_TZ;
}
}
export function formatClock(date: Date, timezone?: string | null, locale = "fa-IR") {
const tz = resolveTimezone(timezone);
return new Intl.DateTimeFormat(locale, {
timeZone: tz,
hour: "2-digit",
minute: "2-digit",
second: "2-digit",
hour12: false,
}).format(date);
}
+1
View File
@@ -1 +1,2 @@
export * from "./brand"; export * from "./brand";
export * from "./timezone";
+34
View File
@@ -0,0 +1,34 @@
const FALLBACK_TZ = "Asia/Tehran";
/** Common mistaken city zones → valid IANA */
const TZ_ALIASES: Record<string, string> = {
"Asia/Esfahan": FALLBACK_TZ,
"Asia/Isfahan": FALLBACK_TZ,
"Asia/Mashhad": FALLBACK_TZ,
"Asia/Shiraz": FALLBACK_TZ,
"Asia/Tabriz": FALLBACK_TZ,
"Iran": FALLBACK_TZ,
};
export function resolveTimezone(input?: string | null): string {
const raw = (input || FALLBACK_TZ).trim() || FALLBACK_TZ;
const candidate = TZ_ALIASES[raw] || raw;
try {
// Throws RangeError if invalid
Intl.DateTimeFormat("en-US", { timeZone: candidate });
return candidate;
} catch {
return FALLBACK_TZ;
}
}
export function formatClock(date: Date, timezone?: string | null, locale = "fa-IR") {
const tz = resolveTimezone(timezone);
return new Intl.DateTimeFormat(locale, {
timeZone: tz,
hour: "2-digit",
minute: "2-digit",
second: "2-digit",
hour12: false,
}).format(date);
}