@open-mercato/core 0.4.8-develop-4e71d95aba → 0.4.8-develop-2acbd97ec3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -13,13 +13,46 @@ const profileResponseSchema = z.object({
13
13
  roles: z.array(z.string())
14
14
  });
15
15
  const passwordSchema = buildPasswordSchema();
16
- const updateSchema = z.object({
16
+ const updateSchemaBase = z.object({
17
17
  email: z.string().email().optional(),
18
+ currentPassword: z.string().trim().min(1).optional(),
18
19
  password: passwordSchema.optional()
19
- }).refine((data) => Boolean(data.email || data.password), {
20
- message: "Provide an email or password.",
21
- path: ["email"]
22
20
  });
21
+ function buildUpdateSchema(translate) {
22
+ return updateSchemaBase.superRefine((data, ctx) => {
23
+ if (!data.email && !data.password) {
24
+ ctx.addIssue({
25
+ code: z.ZodIssueCode.custom,
26
+ message: translate(
27
+ "auth.profile.form.errors.emailOrPasswordRequired",
28
+ "Provide an email or password."
29
+ ),
30
+ path: ["email"]
31
+ });
32
+ }
33
+ if (data.password && !data.currentPassword) {
34
+ ctx.addIssue({
35
+ code: z.ZodIssueCode.custom,
36
+ message: translate(
37
+ "auth.profile.form.errors.currentPasswordRequired",
38
+ "Current password is required."
39
+ ),
40
+ path: ["currentPassword"]
41
+ });
42
+ }
43
+ if (data.currentPassword && !data.password) {
44
+ ctx.addIssue({
45
+ code: z.ZodIssueCode.custom,
46
+ message: translate(
47
+ "auth.profile.form.errors.newPasswordRequired",
48
+ "New password is required."
49
+ ),
50
+ path: ["password"]
51
+ });
52
+ }
53
+ });
54
+ }
55
+ const updateSchema = buildUpdateSchema((_key, fallback) => fallback);
23
56
  const profileUpdateResponseSchema = z.object({
24
57
  ok: z.literal(true),
25
58
  email: z.string().email()
@@ -71,7 +104,7 @@ async function PUT(req) {
71
104
  }
72
105
  try {
73
106
  const body = await req.json().catch(() => ({}));
74
- const parsed = updateSchema.safeParse(body);
107
+ const parsed = buildUpdateSchema(translate).safeParse(body);
75
108
  if (!parsed.success) {
76
109
  return NextResponse.json(
77
110
  {
@@ -82,6 +115,35 @@ async function PUT(req) {
82
115
  );
83
116
  }
84
117
  const container = await createRequestContainer();
118
+ const em = container.resolve("em");
119
+ const authService = container.resolve("authService");
120
+ if (parsed.data.password) {
121
+ const user = await findOneWithDecryption(
122
+ em,
123
+ User,
124
+ { id: auth.sub, deletedAt: null },
125
+ void 0,
126
+ { tenantId: auth.tenantId ?? null, organizationId: auth.orgId ?? null }
127
+ );
128
+ if (!user) {
129
+ return NextResponse.json({ error: translate("auth.users.form.errors.notFound", "User not found") }, { status: 404 });
130
+ }
131
+ const currentPassword = parsed.data.currentPassword?.trim() ?? "";
132
+ const isCurrentPasswordValid = await authService.verifyPassword(user, currentPassword);
133
+ if (!isCurrentPasswordValid) {
134
+ const message = translate(
135
+ "auth.profile.form.errors.currentPasswordInvalid",
136
+ "Current password is incorrect."
137
+ );
138
+ return NextResponse.json(
139
+ {
140
+ error: message,
141
+ issues: [{ path: ["currentPassword"], message }]
142
+ },
143
+ { status: 400 }
144
+ );
145
+ }
146
+ }
85
147
  const commandBus = container.resolve("commandBus");
86
148
  const ctx = buildCommandContext(container, auth, req);
87
149
  const { result } = await commandBus.execute(
@@ -95,7 +157,6 @@ async function PUT(req) {
95
157
  ctx
96
158
  }
97
159
  );
98
- const authService = container.resolve("authService");
99
160
  const roles = await authService.getUserRoles(result, result.tenantId ? String(result.tenantId) : null);
100
161
  const jwt = signJwt({
101
162
  sub: String(result.id),
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../../../../src/modules/auth/api/profile/route.ts"],
4
- "sourcesContent": ["import { NextResponse } from 'next/server'\nimport { z } from 'zod'\nimport type { OpenApiRouteDoc } from '@open-mercato/shared/lib/openapi'\nimport type { CommandBus, CommandRuntimeContext } from '@open-mercato/shared/lib/commands'\nimport { createRequestContainer } from '@open-mercato/shared/lib/di/container'\nimport { getAuthFromRequest } from '@open-mercato/shared/lib/auth/server'\nimport { signJwt } from '@open-mercato/shared/lib/auth/jwt'\nimport { resolveTranslations } from '@open-mercato/shared/lib/i18n/server'\nimport { CrudHttpError } from '@open-mercato/shared/lib/crud/errors'\nimport { AuthService } from '@open-mercato/core/modules/auth/services/authService'\nimport { User } from '@open-mercato/core/modules/auth/data/entities'\nimport type { EntityManager } from '@mikro-orm/postgresql'\nimport { findOneWithDecryption } from '@open-mercato/shared/lib/encryption/find'\nimport { buildPasswordSchema } from '@open-mercato/shared/lib/auth/passwordPolicy'\n\nconst profileResponseSchema = z.object({\n email: z.string().email(),\n roles: z.array(z.string()),\n})\n\nconst passwordSchema = buildPasswordSchema()\n\nconst updateSchema = z.object({\n email: z.string().email().optional(),\n password: passwordSchema.optional(),\n}).refine((data) => Boolean(data.email || data.password), {\n message: 'Provide an email or password.',\n path: ['email'],\n})\n\nconst profileUpdateResponseSchema = z.object({\n ok: z.literal(true),\n email: z.string().email(),\n})\n\nexport const metadata = {\n GET: { requireAuth: true },\n PUT: { requireAuth: true },\n}\n\nfunction buildCommandContext(container: Awaited<ReturnType<typeof createRequestContainer>>, auth: NonNullable<Awaited<ReturnType<typeof getAuthFromRequest>>>, req: Request): CommandRuntimeContext {\n return {\n container,\n auth,\n organizationScope: null,\n selectedOrganizationId: auth.orgId ?? null,\n organizationIds: auth.orgId ? [auth.orgId] : null,\n request: req,\n }\n}\n\nexport async function GET(req: Request) {\n const { translate } = await resolveTranslations()\n const auth = await getAuthFromRequest(req)\n if (!auth?.sub) {\n return NextResponse.json({ error: translate('api.errors.unauthorized', 'Unauthorized') }, { status: 401 })\n }\n try {\n const container = await createRequestContainer()\n const em = (container.resolve('em') as EntityManager)\n const user = await findOneWithDecryption(\n em,\n User,\n { id: auth.sub, deletedAt: null },\n undefined,\n { tenantId: auth.tenantId ?? null, organizationId: auth.orgId ?? null },\n )\n if (!user) {\n return NextResponse.json({ error: translate('auth.users.form.errors.notFound', 'User not found') }, { status: 404 })\n }\n return NextResponse.json({ email: String(user.email), roles: auth.roles ?? [] })\n } catch (err) {\n console.error('auth.profile.load failed', err)\n return NextResponse.json({ error: translate('auth.profile.form.errors.load', 'Failed to load profile.') }, { status: 400 })\n }\n}\n\nexport async function PUT(req: Request) {\n const { translate } = await resolveTranslations()\n const auth = await getAuthFromRequest(req)\n if (!auth?.sub) {\n return NextResponse.json({ error: translate('api.errors.unauthorized', 'Unauthorized') }, { status: 401 })\n }\n try {\n const body = await req.json().catch(() => ({}))\n const parsed = updateSchema.safeParse(body)\n if (!parsed.success) {\n return NextResponse.json(\n {\n error: translate('auth.profile.form.errors.invalid', 'Invalid profile update.'),\n issues: parsed.error.issues,\n },\n { status: 400 },\n )\n }\n const container = await createRequestContainer()\n const commandBus = (container.resolve('commandBus') as CommandBus)\n const ctx = buildCommandContext(container, auth, req)\n const { result } = await commandBus.execute<{ id: string; email?: string; password?: string }, User>(\n 'auth.users.update',\n {\n input: {\n id: auth.sub,\n email: parsed.data.email,\n password: parsed.data.password,\n },\n ctx,\n },\n )\n const authService = container.resolve('authService') as AuthService\n const roles = await authService.getUserRoles(result, result.tenantId ? String(result.tenantId) : null)\n const jwt = signJwt({\n sub: String(result.id),\n tenantId: result.tenantId ? String(result.tenantId) : null,\n orgId: result.organizationId ? String(result.organizationId) : null,\n email: result.email,\n roles,\n })\n const res = NextResponse.json({ ok: true, email: String(result.email) })\n res.cookies.set('auth_token', jwt, {\n httpOnly: true,\n path: '/',\n sameSite: 'lax',\n secure: process.env.NODE_ENV === 'production',\n maxAge: 60 * 60 * 8,\n })\n return res\n } catch (err) {\n if (err instanceof CrudHttpError) {\n return NextResponse.json(err.body, { status: err.status })\n }\n console.error('auth.profile.update failed', err)\n return NextResponse.json({ error: translate('auth.profile.form.errors.save', 'Failed to update profile.') }, { status: 400 })\n }\n}\n\nexport const openApi: OpenApiRouteDoc = {\n tag: 'Authentication & Accounts',\n summary: 'Profile settings',\n methods: {\n GET: {\n summary: 'Get current profile',\n description: 'Returns the email address for the signed-in user.',\n responses: [\n { status: 200, description: 'Profile payload', schema: profileResponseSchema },\n { status: 401, description: 'Unauthorized', schema: z.object({ error: z.string() }) },\n { status: 404, description: 'User not found', schema: z.object({ error: z.string() }) },\n ],\n },\n PUT: {\n summary: 'Update current profile',\n description: 'Updates the email address or password for the signed-in user.',\n requestBody: {\n contentType: 'application/json',\n schema: updateSchema,\n },\n responses: [\n { status: 200, description: 'Profile updated', schema: profileUpdateResponseSchema },\n { status: 400, description: 'Invalid payload', schema: z.object({ error: z.string() }) },\n { status: 401, description: 'Unauthorized', schema: z.object({ error: z.string() }) },\n ],\n },\n },\n}\n"],
5
- "mappings": "AAAA,SAAS,oBAAoB;AAC7B,SAAS,SAAS;AAGlB,SAAS,8BAA8B;AACvC,SAAS,0BAA0B;AACnC,SAAS,eAAe;AACxB,SAAS,2BAA2B;AACpC,SAAS,qBAAqB;AAE9B,SAAS,YAAY;AAErB,SAAS,6BAA6B;AACtC,SAAS,2BAA2B;AAEpC,MAAM,wBAAwB,EAAE,OAAO;AAAA,EACrC,OAAO,EAAE,OAAO,EAAE,MAAM;AAAA,EACxB,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC;AAC3B,CAAC;AAED,MAAM,iBAAiB,oBAAoB;AAE3C,MAAM,eAAe,EAAE,OAAO;AAAA,EAC5B,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,SAAS;AAAA,EACnC,UAAU,eAAe,SAAS;AACpC,CAAC,EAAE,OAAO,CAAC,SAAS,QAAQ,KAAK,SAAS,KAAK,QAAQ,GAAG;AAAA,EACxD,SAAS;AAAA,EACT,MAAM,CAAC,OAAO;AAChB,CAAC;AAED,MAAM,8BAA8B,EAAE,OAAO;AAAA,EAC3C,IAAI,EAAE,QAAQ,IAAI;AAAA,EAClB,OAAO,EAAE,OAAO,EAAE,MAAM;AAC1B,CAAC;AAEM,MAAM,WAAW;AAAA,EACtB,KAAK,EAAE,aAAa,KAAK;AAAA,EACzB,KAAK,EAAE,aAAa,KAAK;AAC3B;AAEA,SAA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,OAAO,EAAE,OAAO,EAAE,CAAC,EAAE;AAAA,MACtF;AAAA,IACF;AAAA,EACF;AACF;",
4
+ "sourcesContent": ["import { NextResponse } from 'next/server'\nimport { z } from 'zod'\nimport type { OpenApiRouteDoc } from '@open-mercato/shared/lib/openapi'\nimport type { CommandBus, CommandRuntimeContext } from '@open-mercato/shared/lib/commands'\nimport { createRequestContainer } from '@open-mercato/shared/lib/di/container'\nimport { getAuthFromRequest } from '@open-mercato/shared/lib/auth/server'\nimport { signJwt } from '@open-mercato/shared/lib/auth/jwt'\nimport { resolveTranslations } from '@open-mercato/shared/lib/i18n/server'\nimport { CrudHttpError } from '@open-mercato/shared/lib/crud/errors'\nimport { AuthService } from '@open-mercato/core/modules/auth/services/authService'\nimport { User } from '@open-mercato/core/modules/auth/data/entities'\nimport type { EntityManager } from '@mikro-orm/postgresql'\nimport { findOneWithDecryption } from '@open-mercato/shared/lib/encryption/find'\nimport { buildPasswordSchema } from '@open-mercato/shared/lib/auth/passwordPolicy'\n\nconst profileResponseSchema = z.object({\n email: z.string().email(),\n roles: z.array(z.string()),\n})\n\nconst passwordSchema = buildPasswordSchema()\n\nconst updateSchemaBase = z.object({\n email: z.string().email().optional(),\n currentPassword: z.string().trim().min(1).optional(),\n password: passwordSchema.optional(),\n})\n\nfunction buildUpdateSchema(translate: (key: string, fallback: string) => string) {\n return updateSchemaBase.superRefine((data, ctx) => {\n if (!data.email && !data.password) {\n ctx.addIssue({\n code: z.ZodIssueCode.custom,\n message: translate(\n 'auth.profile.form.errors.emailOrPasswordRequired',\n 'Provide an email or password.',\n ),\n path: ['email'],\n })\n }\n if (data.password && !data.currentPassword) {\n ctx.addIssue({\n code: z.ZodIssueCode.custom,\n message: translate(\n 'auth.profile.form.errors.currentPasswordRequired',\n 'Current password is required.',\n ),\n path: ['currentPassword'],\n })\n }\n if (data.currentPassword && !data.password) {\n ctx.addIssue({\n code: z.ZodIssueCode.custom,\n message: translate(\n 'auth.profile.form.errors.newPasswordRequired',\n 'New password is required.',\n ),\n path: ['password'],\n })\n }\n })\n}\n\nconst updateSchema = buildUpdateSchema((_key, fallback) => fallback)\n\nconst profileUpdateResponseSchema = z.object({\n ok: z.literal(true),\n email: z.string().email(),\n})\n\nexport const metadata = {\n GET: { requireAuth: true },\n PUT: { requireAuth: true },\n}\n\nfunction buildCommandContext(container: Awaited<ReturnType<typeof createRequestContainer>>, auth: NonNullable<Awaited<ReturnType<typeof getAuthFromRequest>>>, req: Request): CommandRuntimeContext {\n return {\n container,\n auth,\n organizationScope: null,\n selectedOrganizationId: auth.orgId ?? null,\n organizationIds: auth.orgId ? [auth.orgId] : null,\n request: req,\n }\n}\n\nexport async function GET(req: Request) {\n const { translate } = await resolveTranslations()\n const auth = await getAuthFromRequest(req)\n if (!auth?.sub) {\n return NextResponse.json({ error: translate('api.errors.unauthorized', 'Unauthorized') }, { status: 401 })\n }\n try {\n const container = await createRequestContainer()\n const em = (container.resolve('em') as EntityManager)\n const user = await findOneWithDecryption(\n em,\n User,\n { id: auth.sub, deletedAt: null },\n undefined,\n { tenantId: auth.tenantId ?? null, organizationId: auth.orgId ?? null },\n )\n if (!user) {\n return NextResponse.json({ error: translate('auth.users.form.errors.notFound', 'User not found') }, { status: 404 })\n }\n return NextResponse.json({ email: String(user.email), roles: auth.roles ?? [] })\n } catch (err) {\n console.error('auth.profile.load failed', err)\n return NextResponse.json({ error: translate('auth.profile.form.errors.load', 'Failed to load profile.') }, { status: 400 })\n }\n}\n\nexport async function PUT(req: Request) {\n const { translate } = await resolveTranslations()\n const auth = await getAuthFromRequest(req)\n if (!auth?.sub) {\n return NextResponse.json({ error: translate('api.errors.unauthorized', 'Unauthorized') }, { status: 401 })\n }\n try {\n const body = await req.json().catch(() => ({}))\n const parsed = buildUpdateSchema(translate).safeParse(body)\n if (!parsed.success) {\n return NextResponse.json(\n {\n error: translate('auth.profile.form.errors.invalid', 'Invalid profile update.'),\n issues: parsed.error.issues,\n },\n { status: 400 },\n )\n }\n const container = await createRequestContainer()\n const em = (container.resolve('em') as EntityManager)\n const authService = container.resolve('authService') as AuthService\n if (parsed.data.password) {\n const user = await findOneWithDecryption(\n em,\n User,\n { id: auth.sub, deletedAt: null },\n undefined,\n { tenantId: auth.tenantId ?? null, organizationId: auth.orgId ?? null },\n )\n if (!user) {\n return NextResponse.json({ error: translate('auth.users.form.errors.notFound', 'User not found') }, { status: 404 })\n }\n const currentPassword = parsed.data.currentPassword?.trim() ?? ''\n const isCurrentPasswordValid = await authService.verifyPassword(user, currentPassword)\n if (!isCurrentPasswordValid) {\n const message = translate(\n 'auth.profile.form.errors.currentPasswordInvalid',\n 'Current password is incorrect.',\n )\n return NextResponse.json(\n {\n error: message,\n issues: [{ path: ['currentPassword'], message }],\n },\n { status: 400 },\n )\n }\n }\n const commandBus = (container.resolve('commandBus') as CommandBus)\n const ctx = buildCommandContext(container, auth, req)\n const { result } = await commandBus.execute<{ id: string; email?: string; password?: string }, User>(\n 'auth.users.update',\n {\n input: {\n id: auth.sub,\n email: parsed.data.email,\n password: parsed.data.password,\n },\n ctx,\n },\n )\n const roles = await authService.getUserRoles(result, result.tenantId ? String(result.tenantId) : null)\n const jwt = signJwt({\n sub: String(result.id),\n tenantId: result.tenantId ? String(result.tenantId) : null,\n orgId: result.organizationId ? String(result.organizationId) : null,\n email: result.email,\n roles,\n })\n const res = NextResponse.json({ ok: true, email: String(result.email) })\n res.cookies.set('auth_token', jwt, {\n httpOnly: true,\n path: '/',\n sameSite: 'lax',\n secure: process.env.NODE_ENV === 'production',\n maxAge: 60 * 60 * 8,\n })\n return res\n } catch (err) {\n if (err instanceof CrudHttpError) {\n return NextResponse.json(err.body, { status: err.status })\n }\n console.error('auth.profile.update failed', err)\n return NextResponse.json({ error: translate('auth.profile.form.errors.save', 'Failed to update profile.') }, { status: 400 })\n }\n}\n\nexport const openApi: OpenApiRouteDoc = {\n tag: 'Authentication & Accounts',\n summary: 'Profile settings',\n methods: {\n GET: {\n summary: 'Get current profile',\n description: 'Returns the email address for the signed-in user.',\n responses: [\n { status: 200, description: 'Profile payload', schema: profileResponseSchema },\n { status: 401, description: 'Unauthorized', schema: z.object({ error: z.string() }) },\n { status: 404, description: 'User not found', schema: z.object({ error: z.string() }) },\n ],\n },\n PUT: {\n summary: 'Update current profile',\n description: 'Updates the email address or password for the signed-in user.',\n requestBody: {\n contentType: 'application/json',\n schema: updateSchema,\n },\n responses: [\n { status: 200, description: 'Profile updated', schema: profileUpdateResponseSchema },\n { status: 400, description: 'Invalid payload', schema: z.object({ error: z.string() }) },\n { status: 401, description: 'Unauthorized', schema: z.object({ error: z.string() }) },\n ],\n },\n },\n}\n"],
5
+ "mappings": "AAAA,SAAS,oBAAoB;AAC7B,SAAS,SAAS;AAGlB,SAAS,8BAA8B;AACvC,SAAS,0BAA0B;AACnC,SAAS,eAAe;AACxB,SAAS,2BAA2B;AACpC,SAAS,qBAAqB;AAE9B,SAAS,YAAY;AAErB,SAAS,6BAA6B;AACtC,SAAS,2BAA2B;AAEpC,MAAM,wBAAwB,EAAE,OAAO;AAAA,EACrC,OAAO,EAAE,OAAO,EAAE,MAAM;AAAA,EACxB,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC;AAC3B,CAAC;AAED,MAAM,iBAAiB,oBAAoB;AAE3C,MAAM,mBAAmB,EAAE,OAAO;AAAA,EAChC,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,SAAS;AAAA,EACnC,iBAAiB,EAAE,OAAO,EAAE,KAAK,EAAE,IAAI,CAAC,EAAE,SAAS;AAAA,EACnD,UAAU,eAAe,SAAS;AACpC,CAAC;AAED,SAAS,kBAAkB,WAAsD;AAC/E,SAAO,iBAAiB,YAAY,CAAC,MAAM,QAAQ;AACjD,QAAI,CAAC,KAAK,SAAS,CAAC,KAAK,UAAU;AACjC,UAAI,SAAS;AAAA,QACX,MAAM,EAAE,aAAa;AAAA,QACrB,SAAS;AAAA,UACP;AAAA,UACA;AAAA,QACF;AAAA,QACA,MAAM,CAAC,OAAO;AAAA,MAChB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;AAAA,MACV;AAAA,MACA,WAAW;AAAA,QACT,EAAE,QAAQ,KAAK,aAAa,mBAAmB,QAAQ,4BAA4B;AAAA,QACnF,EAAE,QAAQ,KAAK,aAAa,mBAAmB,QAAQ,EAAE,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,CAAC,EAAE;AAAA,QACvF,EAAE,QAAQ,KAAK,aAAa,gBAAgB,QAAQ,EAAE,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,CAAC,EAAE;AAAA,MACtF;AAAA,IACF;AAAA,EACF;AACF;",
6
6
  "names": []
7
7
  }
@@ -51,13 +51,22 @@ function AuthProfilePage() {
51
51
  }, [t]);
52
52
  const fields = React.useMemo(() => [
53
53
  { id: "email", label: t("auth.profile.form.email", "Email"), type: "text", required: true },
54
+ {
55
+ id: "currentPassword",
56
+ label: t("auth.profile.form.currentPassword", "Current password"),
57
+ type: "password"
58
+ },
54
59
  {
55
60
  id: "password",
56
61
  label: t("auth.profile.form.password", "New password"),
57
- type: "text",
62
+ type: "password",
58
63
  description: passwordDescription
59
64
  },
60
- { id: "confirmPassword", label: t("auth.profile.form.confirmPassword", "Confirm new password"), type: "text" }
65
+ {
66
+ id: "confirmPassword",
67
+ label: t("auth.profile.form.confirmPassword", "Confirm new password"),
68
+ type: "password"
69
+ }
61
70
  ], [passwordDescription, t]);
62
71
  const schema = React.useMemo(() => {
63
72
  const passwordSchema = buildPasswordSchema({
@@ -67,12 +76,36 @@ function AuthProfilePage() {
67
76
  const optionalPasswordSchema = z.union([z.literal(""), passwordSchema]).optional();
68
77
  return z.object({
69
78
  email: z.string().trim().min(1, t("auth.profile.form.errors.emailRequired", "Email is required.")),
79
+ currentPassword: z.string().optional(),
70
80
  password: optionalPasswordSchema,
71
81
  confirmPassword: z.string().optional()
72
82
  }).superRefine((values, ctx) => {
83
+ const currentPassword = values.currentPassword?.trim() ?? "";
73
84
  const password = values.password?.trim() ?? "";
74
85
  const confirmPassword = values.confirmPassword?.trim() ?? "";
75
- if ((password || confirmPassword) && password !== confirmPassword) {
86
+ const hasPasswordIntent = Boolean(currentPassword || password || confirmPassword);
87
+ if (hasPasswordIntent && !currentPassword) {
88
+ ctx.addIssue({
89
+ code: z.ZodIssueCode.custom,
90
+ message: t("auth.profile.form.errors.currentPasswordRequired", "Current password is required."),
91
+ path: ["currentPassword"]
92
+ });
93
+ }
94
+ if (hasPasswordIntent && !password) {
95
+ ctx.addIssue({
96
+ code: z.ZodIssueCode.custom,
97
+ message: t("auth.profile.form.errors.newPasswordRequired", "New password is required."),
98
+ path: ["password"]
99
+ });
100
+ }
101
+ if (hasPasswordIntent && !confirmPassword) {
102
+ ctx.addIssue({
103
+ code: z.ZodIssueCode.custom,
104
+ message: t("auth.profile.form.errors.confirmPasswordRequired", "Please confirm the new password."),
105
+ path: ["confirmPassword"]
106
+ });
107
+ }
108
+ if (password && confirmPassword && password !== confirmPassword) {
76
109
  ctx.addIssue({
77
110
  code: z.ZodIssueCode.custom,
78
111
  message: t("auth.profile.form.errors.passwordMismatch", "Passwords do not match."),
@@ -83,12 +116,14 @@ function AuthProfilePage() {
83
116
  }, [passwordPolicy, t]);
84
117
  const handleSubmit = React.useCallback(async (values) => {
85
118
  const nextEmail = values.email?.trim() ?? "";
119
+ const currentPassword = values.currentPassword?.trim() ?? "";
86
120
  const password = values.password?.trim() ?? "";
87
121
  if (!password && nextEmail === email) {
88
122
  throw createCrudFormError(t("auth.profile.form.errors.noChanges", "No changes to save."));
89
123
  }
90
124
  const payload = { email: nextEmail };
91
125
  if (password) payload.password = password;
126
+ if (password) payload.currentPassword = currentPassword;
92
127
  const result = await readApiResultOrThrow(
93
128
  "/api/auth/profile",
94
129
  {
@@ -123,6 +158,7 @@ function AuthProfilePage() {
123
158
  fields,
124
159
  initialValues: {
125
160
  email,
161
+ currentPassword: "",
126
162
  password: "",
127
163
  confirmPassword: ""
128
164
  },
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../../../../../src/modules/auth/backend/auth/profile/page.tsx"],
4
- "sourcesContent": ["\"use client\"\nimport * as React from 'react'\nimport { useRouter } from 'next/navigation'\nimport { z } from 'zod'\nimport { Save } from 'lucide-react'\nimport { Page, PageBody } from '@open-mercato/ui/backend/Page'\nimport { CrudForm, type CrudField } from '@open-mercato/ui/backend/CrudForm'\nimport { apiCall, readApiResultOrThrow } from '@open-mercato/ui/backend/utils/apiCall'\nimport { createCrudFormError } from '@open-mercato/ui/backend/utils/serverErrors'\nimport { flash } from '@open-mercato/ui/backend/FlashMessages'\nimport { LoadingMessage, ErrorMessage } from '@open-mercato/ui/backend/detail'\nimport { Button } from '@open-mercato/ui/primitives/button'\nimport { useT } from '@open-mercato/shared/lib/i18n/context'\nimport { buildPasswordSchema, formatPasswordRequirements, getPasswordPolicy } from '@open-mercato/shared/lib/auth/passwordPolicy'\n\ntype ProfileResponse = {\n email?: string | null\n}\n\ntype ProfileUpdateResponse = {\n ok?: boolean\n email?: string | null\n}\n\ntype ProfileFormValues = {\n email: string\n password?: string\n confirmPassword?: string\n}\n\nexport default function AuthProfilePage() {\n const t = useT()\n const router = useRouter()\n const [loading, setLoading] = React.useState(true)\n const [error, setError] = React.useState<string | null>(null)\n const [email, setEmail] = React.useState('')\n const [formKey, setFormKey] = React.useState(0)\n const formId = React.useId()\n const passwordPolicy = React.useMemo(() => getPasswordPolicy(), [])\n const passwordRequirements = React.useMemo(\n () => formatPasswordRequirements(passwordPolicy, t),\n [passwordPolicy, t],\n )\n const passwordDescription = React.useMemo(() => (\n passwordRequirements\n ? t('auth.password.requirements.help', 'Password requirements: {requirements}', { requirements: passwordRequirements })\n : undefined\n ), [passwordRequirements, t])\n\n React.useEffect(() => {\n let cancelled = false\n async function load() {\n setLoading(true)\n setError(null)\n try {\n const { ok, result } = await apiCall<ProfileResponse>('/api/auth/profile')\n if (!ok) throw new Error('load_failed')\n const resolvedEmail = typeof result?.email === 'string' ? result.email : ''\n if (!cancelled) setEmail(resolvedEmail)\n } catch (err) {\n console.error('Failed to load auth profile', err)\n if (!cancelled) setError(t('auth.profile.form.errors.load', 'Failed to load profile.'))\n } finally {\n if (!cancelled) setLoading(false)\n }\n }\n load()\n return () => { cancelled = true }\n }, [t])\n\n const fields = React.useMemo<CrudField[]>(() => [\n { id: 'email', label: t('auth.profile.form.email', 'Email'), type: 'text', required: true },\n {\n id: 'password',\n label: t('auth.profile.form.password', 'New password'),\n type: 'text',\n description: passwordDescription,\n },\n { id: 'confirmPassword', label: t('auth.profile.form.confirmPassword', 'Confirm new password'), type: 'text' },\n ], [passwordDescription, t])\n\n const schema = React.useMemo(() => {\n const passwordSchema = buildPasswordSchema({\n policy: passwordPolicy,\n message: t('auth.profile.form.errors.passwordRequirements', 'Password must meet the requirements.'),\n })\n const optionalPasswordSchema = z.union([z.literal(''), passwordSchema]).optional()\n return z.object({\n email: z.string().trim().min(1, t('auth.profile.form.errors.emailRequired', 'Email is required.')),\n password: optionalPasswordSchema,\n confirmPassword: z.string().optional(),\n }).superRefine((values, ctx) => {\n const password = values.password?.trim() ?? ''\n const confirmPassword = values.confirmPassword?.trim() ?? ''\n if ((password || confirmPassword) && password !== confirmPassword) {\n ctx.addIssue({\n code: z.ZodIssueCode.custom,\n message: t('auth.profile.form.errors.passwordMismatch', 'Passwords do not match.'),\n path: ['confirmPassword'],\n })\n }\n })\n }, [passwordPolicy, t])\n\n const handleSubmit = React.useCallback(async (values: ProfileFormValues) => {\n const nextEmail = values.email?.trim() ?? ''\n const password = values.password?.trim() ?? ''\n\n if (!password && nextEmail === email) {\n throw createCrudFormError(t('auth.profile.form.errors.noChanges', 'No changes to save.'))\n }\n\n const payload: { email: string; password?: string } = { email: nextEmail }\n if (password) payload.password = password\n\n const result = await readApiResultOrThrow<ProfileUpdateResponse>(\n '/api/auth/profile',\n {\n method: 'PUT',\n headers: { 'content-type': 'application/json' },\n body: JSON.stringify(payload),\n },\n { errorMessage: t('auth.profile.form.errors.save', 'Failed to update profile.') },\n )\n\n const resolvedEmail = typeof result?.email === 'string' ? result.email : nextEmail\n setEmail(resolvedEmail)\n setFormKey((prev) => prev + 1)\n flash(t('auth.profile.form.success', 'Profile updated.'), 'success')\n router.refresh()\n }, [email, router, t])\n\n return (\n <Page>\n <PageBody>\n {loading ? (\n <LoadingMessage label={t('auth.profile.form.loading', 'Loading profile...')} />\n ) : error ? (\n <ErrorMessage label={error} />\n ) : (\n <section className=\"space-y-6 rounded-lg border bg-background p-6\">\n <header className=\"flex flex-col gap-4 sm:flex-row sm:items-start sm:justify-between\">\n <div className=\"space-y-1\">\n <h2 className=\"text-lg font-semibold\">{t('auth.profile.title', 'Profile')}</h2>\n <p className=\"text-sm text-muted-foreground\">\n {t('auth.profile.subtitle', 'Change password')}\n </p>\n </div>\n <Button type=\"submit\" form={formId}>\n <Save className=\"size-4 mr-2\" />\n {t('auth.profile.form.save', 'Save changes')}\n </Button>\n </header>\n <CrudForm<ProfileFormValues>\n key={formKey}\n formId={formId}\n schema={schema}\n fields={fields}\n initialValues={{\n email,\n password: '',\n confirmPassword: '',\n }}\n submitLabel={t('auth.profile.form.save', 'Save changes')}\n onSubmit={handleSubmit}\n embedded\n hideFooterActions\n />\n </section>\n )}\n </PageBody>\n </Page>\n )\n}\n"],
5
- "mappings": ";AAwIU,cAMI,YANJ;AAvIV,YAAY,WAAW;AACvB,SAAS,iBAAiB;AAC1B,SAAS,SAAS;AAClB,SAAS,YAAY;AACrB,SAAS,MAAM,gBAAgB;AAC/B,SAAS,gBAAgC;AACzC,SAAS,SAAS,4BAA4B;AAC9C,SAAS,2BAA2B;AACpC,SAAS,aAAa;AACtB,SAAS,gBAAgB,oBAAoB;AAC7C,SAAS,cAAc;AACvB,SAAS,YAAY;AACrB,SAAS,qBAAqB,4BAA4B,yBAAyB;AAiBpE,SAAR,kBAAmC;AACxC,QAAM,IAAI,KAAK;AACf,QAAM,SAAS,UAAU;AACzB,QAAM,CAAC,SAAS,UAAU,IAAI,MAAM,SAAS,IAAI;AACjD,QAAM,CAAC,OAAO,QAAQ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AAQ;AAAA,QACR,mBAAiB;AAAA;AAAA,MAZZ;AAAA,IAaP;AAAA,KACF,GAEJ,GACF;AAEJ;",
4
+ "sourcesContent": ["\"use client\"\nimport * as React from 'react'\nimport { useRouter } from 'next/navigation'\nimport { z } from 'zod'\nimport { Save } from 'lucide-react'\nimport { Page, PageBody } from '@open-mercato/ui/backend/Page'\nimport { CrudForm, type CrudField } from '@open-mercato/ui/backend/CrudForm'\nimport { apiCall, readApiResultOrThrow } from '@open-mercato/ui/backend/utils/apiCall'\nimport { createCrudFormError } from '@open-mercato/ui/backend/utils/serverErrors'\nimport { flash } from '@open-mercato/ui/backend/FlashMessages'\nimport { LoadingMessage, ErrorMessage } from '@open-mercato/ui/backend/detail'\nimport { Button } from '@open-mercato/ui/primitives/button'\nimport { useT } from '@open-mercato/shared/lib/i18n/context'\nimport { buildPasswordSchema, formatPasswordRequirements, getPasswordPolicy } from '@open-mercato/shared/lib/auth/passwordPolicy'\n\ntype ProfileResponse = {\n email?: string | null\n}\n\ntype ProfileUpdateResponse = {\n ok?: boolean\n email?: string | null\n}\n\ntype ProfileFormValues = {\n email: string\n currentPassword?: string\n password?: string\n confirmPassword?: string\n}\n\nexport default function AuthProfilePage() {\n const t = useT()\n const router = useRouter()\n const [loading, setLoading] = React.useState(true)\n const [error, setError] = React.useState<string | null>(null)\n const [email, setEmail] = React.useState('')\n const [formKey, setFormKey] = React.useState(0)\n const formId = React.useId()\n const passwordPolicy = React.useMemo(() => getPasswordPolicy(), [])\n const passwordRequirements = React.useMemo(\n () => formatPasswordRequirements(passwordPolicy, t),\n [passwordPolicy, t],\n )\n const passwordDescription = React.useMemo(() => (\n passwordRequirements\n ? t('auth.password.requirements.help', 'Password requirements: {requirements}', { requirements: passwordRequirements })\n : undefined\n ), [passwordRequirements, t])\n\n React.useEffect(() => {\n let cancelled = false\n async function load() {\n setLoading(true)\n setError(null)\n try {\n const { ok, result } = await apiCall<ProfileResponse>('/api/auth/profile')\n if (!ok) throw new Error('load_failed')\n const resolvedEmail = typeof result?.email === 'string' ? result.email : ''\n if (!cancelled) setEmail(resolvedEmail)\n } catch (err) {\n console.error('Failed to load auth profile', err)\n if (!cancelled) setError(t('auth.profile.form.errors.load', 'Failed to load profile.'))\n } finally {\n if (!cancelled) setLoading(false)\n }\n }\n load()\n return () => { cancelled = true }\n }, [t])\n\n const fields = React.useMemo<CrudField[]>(() => [\n { id: 'email', label: t('auth.profile.form.email', 'Email'), type: 'text', required: true },\n {\n id: 'currentPassword',\n label: t('auth.profile.form.currentPassword', 'Current password'),\n type: 'password',\n },\n {\n id: 'password',\n label: t('auth.profile.form.password', 'New password'),\n type: 'password',\n description: passwordDescription,\n },\n {\n id: 'confirmPassword',\n label: t('auth.profile.form.confirmPassword', 'Confirm new password'),\n type: 'password',\n },\n ], [passwordDescription, t])\n\n const schema = React.useMemo(() => {\n const passwordSchema = buildPasswordSchema({\n policy: passwordPolicy,\n message: t('auth.profile.form.errors.passwordRequirements', 'Password must meet the requirements.'),\n })\n const optionalPasswordSchema = z.union([z.literal(''), passwordSchema]).optional()\n return z.object({\n email: z.string().trim().min(1, t('auth.profile.form.errors.emailRequired', 'Email is required.')),\n currentPassword: z.string().optional(),\n password: optionalPasswordSchema,\n confirmPassword: z.string().optional(),\n }).superRefine((values, ctx) => {\n const currentPassword = values.currentPassword?.trim() ?? ''\n const password = values.password?.trim() ?? ''\n const confirmPassword = values.confirmPassword?.trim() ?? ''\n const hasPasswordIntent = Boolean(currentPassword || password || confirmPassword)\n\n if (hasPasswordIntent && !currentPassword) {\n ctx.addIssue({\n code: z.ZodIssueCode.custom,\n message: t('auth.profile.form.errors.currentPasswordRequired', 'Current password is required.'),\n path: ['currentPassword'],\n })\n }\n if (hasPasswordIntent && !password) {\n ctx.addIssue({\n code: z.ZodIssueCode.custom,\n message: t('auth.profile.form.errors.newPasswordRequired', 'New password is required.'),\n path: ['password'],\n })\n }\n if (hasPasswordIntent && !confirmPassword) {\n ctx.addIssue({\n code: z.ZodIssueCode.custom,\n message: t('auth.profile.form.errors.confirmPasswordRequired', 'Please confirm the new password.'),\n path: ['confirmPassword'],\n })\n }\n if (password && confirmPassword && password !== confirmPassword) {\n ctx.addIssue({\n code: z.ZodIssueCode.custom,\n message: t('auth.profile.form.errors.passwordMismatch', 'Passwords do not match.'),\n path: ['confirmPassword'],\n })\n }\n })\n }, [passwordPolicy, t])\n\n const handleSubmit = React.useCallback(async (values: ProfileFormValues) => {\n const nextEmail = values.email?.trim() ?? ''\n const currentPassword = values.currentPassword?.trim() ?? ''\n const password = values.password?.trim() ?? ''\n\n if (!password && nextEmail === email) {\n throw createCrudFormError(t('auth.profile.form.errors.noChanges', 'No changes to save.'))\n }\n\n const payload: { email: string; currentPassword?: string; password?: string } = { email: nextEmail }\n if (password) payload.password = password\n if (password) payload.currentPassword = currentPassword\n\n const result = await readApiResultOrThrow<ProfileUpdateResponse>(\n '/api/auth/profile',\n {\n method: 'PUT',\n headers: { 'content-type': 'application/json' },\n body: JSON.stringify(payload),\n },\n { errorMessage: t('auth.profile.form.errors.save', 'Failed to update profile.') },\n )\n\n const resolvedEmail = typeof result?.email === 'string' ? result.email : nextEmail\n setEmail(resolvedEmail)\n setFormKey((prev) => prev + 1)\n flash(t('auth.profile.form.success', 'Profile updated.'), 'success')\n router.refresh()\n }, [email, router, t])\n\n return (\n <Page>\n <PageBody>\n {loading ? (\n <LoadingMessage label={t('auth.profile.form.loading', 'Loading profile...')} />\n ) : error ? (\n <ErrorMessage label={error} />\n ) : (\n <section className=\"space-y-6 rounded-lg border bg-background p-6\">\n <header className=\"flex flex-col gap-4 sm:flex-row sm:items-start sm:justify-between\">\n <div className=\"space-y-1\">\n <h2 className=\"text-lg font-semibold\">{t('auth.profile.title', 'Profile')}</h2>\n <p className=\"text-sm text-muted-foreground\">\n {t('auth.profile.subtitle', 'Change password')}\n </p>\n </div>\n <Button type=\"submit\" form={formId}>\n <Save className=\"size-4 mr-2\" />\n {t('auth.profile.form.save', 'Save changes')}\n </Button>\n </header>\n <CrudForm<ProfileFormValues>\n key={formKey}\n formId={formId}\n schema={schema}\n fields={fields}\n initialValues={{\n email,\n currentPassword: '',\n password: '',\n confirmPassword: '',\n }}\n submitLabel={t('auth.profile.form.save', 'Save changes')}\n onSubmit={handleSubmit}\n embedded\n hideFooterActions\n />\n </section>\n )}\n </PageBody>\n </Page>\n )\n}\n"],
5
+ "mappings": ";AA6KU,cAMI,YANJ;AA5KV,YAAY,WAAW;AACvB,SAAS,iBAAiB;AAC1B,SAAS,SAAS;AAClB,SAAS,YAAY;AACrB,SAAS,MAAM,gBAAgB;AAC/B,SAAS,gBAAgC;AACzC,SAAS,SAAS,4BAA4B;AAC9C,SAAS,2BAA2B;AACpC,SAAS,aAAa;AACtB,SAAS,gBAAgB,oBAAoB;AAC7C,SAAS,cAAc;AACvB,SAAS,YAAY;AACrB,SAAS,qBAAqB,4BAA4B,yBAAyB;AAkBpE,SAAR,kBAAmC;AACxC,QAAM,IAAI,KAAK;AACf,QAAM,SAAS,UAAU;AACzB,QAAM,CAAC,SAAS,UAAU,IAAI,MAAM,SAAS,IAAI;AACjD,QAAM,CAAC,OAAO,QAAQ,IAAI,MAAM,SAAwB,IAAI;AAC5D,QAAM,CAAC,OAAO,QAAQ,IAAI,MAAM,SAAS,EAAE;AAC3C,QAAM,CAAC,SAAS,UAAU,IAAI,MAAM,SAAS,CAAC;AAC9C,QAAM,SAAS,MAAM,MAAM;AAC3B,QAAM,iBAAiB,MAAM,QAAQ,MAAM,kBAAkB,GAAG,CAAC,CAAC;AAClE,QAAM,uBAAuB,MAAM;AAAA,IACjC,MAAM,2BAA2B,gBAAgB,CAAC;AAAA,IAClD,CAAC,gBAAgB,CAAC;AAAA,EACpB;AACA,QAAM,sBAAsB,MAAM,QAAQ,MACxC,uBACI,EAAE,mCAAmC,yCAAyC,EAAE,cAAc,qBAAqB,CAAC,IACpH,QACH,CAAC,sBAAsB,CAAC,CAAC;AAE5B,QAAM,UAAU,MAAM;AACpB,QAAI,YAAY;A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C/C;AAAA,SACF;AAAA,MACA,qBAAC,UAAO,MAAK,UAAS,MAAM,QAC1B;AAAA,4BAAC,QAAK,WAAU,eAAc;AAAA,QAC7B,EAAE,0BAA0B,cAAc;AAAA,SAC7C;AAAA,OACF;AAAA,IACA;AAAA,MAAC;AAAA;AAAA,QAEC;AAAA,QACA;AAAA,QACA;AAAA,QACA,eAAe;AAAA,UACb;AAAA,UACA,iBAAiB;AAAA,UACjB,UAAU;AAAA,UACV,iBAAiB;AAAA,QACnB;AAAA,QACA,aAAa,EAAE,0BAA0B,cAAc;AAAA,QACvD,UAAU;AAAA,QACV,UAAQ;AAAA,QACR,mBAAiB;AAAA;AAAA,MAbZ;AAAA,IAcP;AAAA,KACF,GAEJ,GACF;AAEJ;",
6
6
  "names": []
7
7
  }
@@ -50,13 +50,22 @@ function ProfileChangePasswordPage() {
50
50
  }, [t]);
51
51
  const fields = React.useMemo(() => [
52
52
  { id: "email", label: t("auth.profile.form.email", "Email"), type: "text", required: true },
53
+ {
54
+ id: "currentPassword",
55
+ label: t("auth.profile.form.currentPassword", "Current password"),
56
+ type: "password"
57
+ },
53
58
  {
54
59
  id: "password",
55
60
  label: t("auth.profile.form.password", "New password"),
56
- type: "text",
61
+ type: "password",
57
62
  description: passwordDescription
58
63
  },
59
- { id: "confirmPassword", label: t("auth.profile.form.confirmPassword", "Confirm new password"), type: "text" }
64
+ {
65
+ id: "confirmPassword",
66
+ label: t("auth.profile.form.confirmPassword", "Confirm new password"),
67
+ type: "password"
68
+ }
60
69
  ], [passwordDescription, t]);
61
70
  const schema = React.useMemo(() => {
62
71
  const passwordSchema = buildPasswordSchema({
@@ -66,12 +75,36 @@ function ProfileChangePasswordPage() {
66
75
  const optionalPasswordSchema = z.union([z.literal(""), passwordSchema]).optional();
67
76
  return z.object({
68
77
  email: z.string().trim().min(1, t("auth.profile.form.errors.emailRequired", "Email is required.")),
78
+ currentPassword: z.string().optional(),
69
79
  password: optionalPasswordSchema,
70
80
  confirmPassword: z.string().optional()
71
81
  }).superRefine((values, ctx) => {
82
+ const currentPassword = values.currentPassword?.trim() ?? "";
72
83
  const password = values.password?.trim() ?? "";
73
84
  const confirmPassword = values.confirmPassword?.trim() ?? "";
74
- if ((password || confirmPassword) && password !== confirmPassword) {
85
+ const hasPasswordIntent = Boolean(currentPassword || password || confirmPassword);
86
+ if (hasPasswordIntent && !currentPassword) {
87
+ ctx.addIssue({
88
+ code: z.ZodIssueCode.custom,
89
+ message: t("auth.profile.form.errors.currentPasswordRequired", "Current password is required."),
90
+ path: ["currentPassword"]
91
+ });
92
+ }
93
+ if (hasPasswordIntent && !password) {
94
+ ctx.addIssue({
95
+ code: z.ZodIssueCode.custom,
96
+ message: t("auth.profile.form.errors.newPasswordRequired", "New password is required."),
97
+ path: ["password"]
98
+ });
99
+ }
100
+ if (hasPasswordIntent && !confirmPassword) {
101
+ ctx.addIssue({
102
+ code: z.ZodIssueCode.custom,
103
+ message: t("auth.profile.form.errors.confirmPasswordRequired", "Please confirm the new password."),
104
+ path: ["confirmPassword"]
105
+ });
106
+ }
107
+ if (password && confirmPassword && password !== confirmPassword) {
75
108
  ctx.addIssue({
76
109
  code: z.ZodIssueCode.custom,
77
110
  message: t("auth.profile.form.errors.passwordMismatch", "Passwords do not match."),
@@ -82,12 +115,14 @@ function ProfileChangePasswordPage() {
82
115
  }, [passwordPolicy, t]);
83
116
  const handleSubmit = React.useCallback(async (values) => {
84
117
  const nextEmail = values.email?.trim() ?? "";
118
+ const currentPassword = values.currentPassword?.trim() ?? "";
85
119
  const password = values.password?.trim() ?? "";
86
120
  if (!password && nextEmail === email) {
87
121
  throw createCrudFormError(t("auth.profile.form.errors.noChanges", "No changes to save."));
88
122
  }
89
123
  const payload = { email: nextEmail };
90
124
  if (password) payload.password = password;
125
+ if (password) payload.currentPassword = currentPassword;
91
126
  const result = await readApiResultOrThrow(
92
127
  "/api/auth/profile",
93
128
  {
@@ -128,6 +163,7 @@ function ProfileChangePasswordPage() {
128
163
  fields,
129
164
  initialValues: {
130
165
  email,
166
+ currentPassword: "",
131
167
  password: "",
132
168
  confirmPassword: ""
133
169
  },
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../../../../../src/modules/auth/backend/profile/change-password/page.tsx"],
4
- "sourcesContent": ["'use client'\nimport * as React from 'react'\nimport { useRouter } from 'next/navigation'\nimport { z } from 'zod'\nimport { Save } from 'lucide-react'\nimport { CrudForm, type CrudField } from '@open-mercato/ui/backend/CrudForm'\nimport { apiCall, readApiResultOrThrow } from '@open-mercato/ui/backend/utils/apiCall'\nimport { createCrudFormError } from '@open-mercato/ui/backend/utils/serverErrors'\nimport { flash } from '@open-mercato/ui/backend/FlashMessages'\nimport { LoadingMessage, ErrorMessage } from '@open-mercato/ui/backend/detail'\nimport { Button } from '@open-mercato/ui/primitives/button'\nimport { useT } from '@open-mercato/shared/lib/i18n/context'\nimport { buildPasswordSchema, formatPasswordRequirements, getPasswordPolicy } from '@open-mercato/shared/lib/auth/passwordPolicy'\n\ntype ProfileResponse = {\n email?: string | null\n}\n\ntype ProfileUpdateResponse = {\n ok?: boolean\n email?: string | null\n}\n\ntype ProfileFormValues = {\n email: string\n password?: string\n confirmPassword?: string\n}\n\nexport default function ProfileChangePasswordPage() {\n const t = useT()\n const router = useRouter()\n const [loading, setLoading] = React.useState(true)\n const [error, setError] = React.useState<string | null>(null)\n const [email, setEmail] = React.useState('')\n const [formKey, setFormKey] = React.useState(0)\n const formId = React.useId()\n const passwordPolicy = React.useMemo(() => getPasswordPolicy(), [])\n const passwordRequirements = React.useMemo(\n () => formatPasswordRequirements(passwordPolicy, t),\n [passwordPolicy, t],\n )\n const passwordDescription = React.useMemo(() => (\n passwordRequirements\n ? t('auth.password.requirements.help', 'Password requirements: {requirements}', { requirements: passwordRequirements })\n : undefined\n ), [passwordRequirements, t])\n\n React.useEffect(() => {\n let cancelled = false\n async function load() {\n setLoading(true)\n setError(null)\n try {\n const { ok, result } = await apiCall<ProfileResponse>('/api/auth/profile')\n if (!ok) throw new Error('load_failed')\n const resolvedEmail = typeof result?.email === 'string' ? result.email : ''\n if (!cancelled) setEmail(resolvedEmail)\n } catch (err) {\n console.error('Failed to load auth profile', err)\n if (!cancelled) setError(t('auth.profile.form.errors.load', 'Failed to load profile.'))\n } finally {\n if (!cancelled) setLoading(false)\n }\n }\n load()\n return () => { cancelled = true }\n }, [t])\n\n const fields = React.useMemo<CrudField[]>(() => [\n { id: 'email', label: t('auth.profile.form.email', 'Email'), type: 'text', required: true },\n {\n id: 'password',\n label: t('auth.profile.form.password', 'New password'),\n type: 'text',\n description: passwordDescription,\n },\n { id: 'confirmPassword', label: t('auth.profile.form.confirmPassword', 'Confirm new password'), type: 'text' },\n ], [passwordDescription, t])\n\n const schema = React.useMemo(() => {\n const passwordSchema = buildPasswordSchema({\n policy: passwordPolicy,\n message: t('auth.profile.form.errors.passwordRequirements', 'Password must meet the requirements.'),\n })\n const optionalPasswordSchema = z.union([z.literal(''), passwordSchema]).optional()\n return z.object({\n email: z.string().trim().min(1, t('auth.profile.form.errors.emailRequired', 'Email is required.')),\n password: optionalPasswordSchema,\n confirmPassword: z.string().optional(),\n }).superRefine((values, ctx) => {\n const password = values.password?.trim() ?? ''\n const confirmPassword = values.confirmPassword?.trim() ?? ''\n if ((password || confirmPassword) && password !== confirmPassword) {\n ctx.addIssue({\n code: z.ZodIssueCode.custom,\n message: t('auth.profile.form.errors.passwordMismatch', 'Passwords do not match.'),\n path: ['confirmPassword'],\n })\n }\n })\n }, [passwordPolicy, t])\n\n const handleSubmit = React.useCallback(async (values: ProfileFormValues) => {\n const nextEmail = values.email?.trim() ?? ''\n const password = values.password?.trim() ?? ''\n\n if (!password && nextEmail === email) {\n throw createCrudFormError(t('auth.profile.form.errors.noChanges', 'No changes to save.'))\n }\n\n const payload: { email: string; password?: string } = { email: nextEmail }\n if (password) payload.password = password\n\n const result = await readApiResultOrThrow<ProfileUpdateResponse>(\n '/api/auth/profile',\n {\n method: 'PUT',\n headers: { 'content-type': 'application/json' },\n body: JSON.stringify(payload),\n },\n { errorMessage: t('auth.profile.form.errors.save', 'Failed to update profile.') },\n )\n\n const resolvedEmail = typeof result?.email === 'string' ? result.email : nextEmail\n setEmail(resolvedEmail)\n setFormKey((prev) => prev + 1)\n flash(t('auth.profile.form.success', 'Profile updated.'), 'success')\n router.refresh()\n }, [email, router, t])\n\n if (loading) {\n return <LoadingMessage label={t('auth.profile.form.loading', 'Loading profile...')} />\n }\n\n if (error) {\n return <ErrorMessage label={error} />\n }\n\n return (\n <section className=\"space-y-6 rounded-lg border bg-background p-6 max-w-2xl\">\n <header className=\"flex flex-col gap-4 sm:flex-row sm:items-start sm:justify-between\">\n <div className=\"space-y-1\">\n <h2 className=\"text-lg font-semibold\">{t('auth.changePassword.title', 'Change Password')}</h2>\n <p className=\"text-sm text-muted-foreground\">\n {t('auth.profile.subtitle', 'Change password')}\n </p>\n </div>\n <Button type=\"submit\" form={formId}>\n <Save className=\"size-4 mr-2\" />\n {t('auth.profile.form.save', 'Save changes')}\n </Button>\n </header>\n <CrudForm<ProfileFormValues>\n key={formKey}\n formId={formId}\n schema={schema}\n fields={fields}\n initialValues={{\n email,\n password: '',\n confirmPassword: '',\n }}\n submitLabel={t('auth.profile.form.save', 'Save changes')}\n onSubmit={handleSubmit}\n embedded\n hideFooterActions\n />\n </section>\n )\n}\n"],
5
- "mappings": ";AAoIW,cAUH,YAVG;AAnIX,YAAY,WAAW;AACvB,SAAS,iBAAiB;AAC1B,SAAS,SAAS;AAClB,SAAS,YAAY;AACrB,SAAS,gBAAgC;AACzC,SAAS,SAAS,4BAA4B;AAC9C,SAAS,2BAA2B;AACpC,SAAS,aAAa;AACtB,SAAS,gBAAgB,oBAAoB;AAC7C,SAAS,cAAc;AACvB,SAAS,YAAY;AACrB,SAAS,qBAAqB,4BAA4B,yBAAyB;AAiBpE,SAAR,4BAA6C;AAClD,QAAM,IAAI,KAAK;AACf,QAAM,SAAS,UAAU;AACzB,QAAM,CAAC,SA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AA,UACV,iBAAiB;AAAA,QACnB;AAAA,QACA,aAAa,EAAE,0BAA0B,cAAc;AAAA,QACvD,UAAU;AAAA,QACV,UAAQ;AAAA,QACR,mBAAiB;AAAA;AAAA,MAZZ;AAAA,IAaP;AAAA,KACF;AAEJ;",
4
+ "sourcesContent": ["'use client'\nimport * as React from 'react'\nimport { useRouter } from 'next/navigation'\nimport { z } from 'zod'\nimport { Save } from 'lucide-react'\nimport { CrudForm, type CrudField } from '@open-mercato/ui/backend/CrudForm'\nimport { apiCall, readApiResultOrThrow } from '@open-mercato/ui/backend/utils/apiCall'\nimport { createCrudFormError } from '@open-mercato/ui/backend/utils/serverErrors'\nimport { flash } from '@open-mercato/ui/backend/FlashMessages'\nimport { LoadingMessage, ErrorMessage } from '@open-mercato/ui/backend/detail'\nimport { Button } from '@open-mercato/ui/primitives/button'\nimport { useT } from '@open-mercato/shared/lib/i18n/context'\nimport { buildPasswordSchema, formatPasswordRequirements, getPasswordPolicy } from '@open-mercato/shared/lib/auth/passwordPolicy'\n\ntype ProfileResponse = {\n email?: string | null\n}\n\ntype ProfileUpdateResponse = {\n ok?: boolean\n email?: string | null\n}\n\ntype ProfileFormValues = {\n email: string\n currentPassword?: string\n password?: string\n confirmPassword?: string\n}\n\nexport default function ProfileChangePasswordPage() {\n const t = useT()\n const router = useRouter()\n const [loading, setLoading] = React.useState(true)\n const [error, setError] = React.useState<string | null>(null)\n const [email, setEmail] = React.useState('')\n const [formKey, setFormKey] = React.useState(0)\n const formId = React.useId()\n const passwordPolicy = React.useMemo(() => getPasswordPolicy(), [])\n const passwordRequirements = React.useMemo(\n () => formatPasswordRequirements(passwordPolicy, t),\n [passwordPolicy, t],\n )\n const passwordDescription = React.useMemo(() => (\n passwordRequirements\n ? t('auth.password.requirements.help', 'Password requirements: {requirements}', { requirements: passwordRequirements })\n : undefined\n ), [passwordRequirements, t])\n\n React.useEffect(() => {\n let cancelled = false\n async function load() {\n setLoading(true)\n setError(null)\n try {\n const { ok, result } = await apiCall<ProfileResponse>('/api/auth/profile')\n if (!ok) throw new Error('load_failed')\n const resolvedEmail = typeof result?.email === 'string' ? result.email : ''\n if (!cancelled) setEmail(resolvedEmail)\n } catch (err) {\n console.error('Failed to load auth profile', err)\n if (!cancelled) setError(t('auth.profile.form.errors.load', 'Failed to load profile.'))\n } finally {\n if (!cancelled) setLoading(false)\n }\n }\n load()\n return () => { cancelled = true }\n }, [t])\n\n const fields = React.useMemo<CrudField[]>(() => [\n { id: 'email', label: t('auth.profile.form.email', 'Email'), type: 'text', required: true },\n {\n id: 'currentPassword',\n label: t('auth.profile.form.currentPassword', 'Current password'),\n type: 'password',\n },\n {\n id: 'password',\n label: t('auth.profile.form.password', 'New password'),\n type: 'password',\n description: passwordDescription,\n },\n {\n id: 'confirmPassword',\n label: t('auth.profile.form.confirmPassword', 'Confirm new password'),\n type: 'password',\n },\n ], [passwordDescription, t])\n\n const schema = React.useMemo(() => {\n const passwordSchema = buildPasswordSchema({\n policy: passwordPolicy,\n message: t('auth.profile.form.errors.passwordRequirements', 'Password must meet the requirements.'),\n })\n const optionalPasswordSchema = z.union([z.literal(''), passwordSchema]).optional()\n return z.object({\n email: z.string().trim().min(1, t('auth.profile.form.errors.emailRequired', 'Email is required.')),\n currentPassword: z.string().optional(),\n password: optionalPasswordSchema,\n confirmPassword: z.string().optional(),\n }).superRefine((values, ctx) => {\n const currentPassword = values.currentPassword?.trim() ?? ''\n const password = values.password?.trim() ?? ''\n const confirmPassword = values.confirmPassword?.trim() ?? ''\n const hasPasswordIntent = Boolean(currentPassword || password || confirmPassword)\n\n if (hasPasswordIntent && !currentPassword) {\n ctx.addIssue({\n code: z.ZodIssueCode.custom,\n message: t('auth.profile.form.errors.currentPasswordRequired', 'Current password is required.'),\n path: ['currentPassword'],\n })\n }\n if (hasPasswordIntent && !password) {\n ctx.addIssue({\n code: z.ZodIssueCode.custom,\n message: t('auth.profile.form.errors.newPasswordRequired', 'New password is required.'),\n path: ['password'],\n })\n }\n if (hasPasswordIntent && !confirmPassword) {\n ctx.addIssue({\n code: z.ZodIssueCode.custom,\n message: t('auth.profile.form.errors.confirmPasswordRequired', 'Please confirm the new password.'),\n path: ['confirmPassword'],\n })\n }\n if (password && confirmPassword && password !== confirmPassword) {\n ctx.addIssue({\n code: z.ZodIssueCode.custom,\n message: t('auth.profile.form.errors.passwordMismatch', 'Passwords do not match.'),\n path: ['confirmPassword'],\n })\n }\n })\n }, [passwordPolicy, t])\n\n const handleSubmit = React.useCallback(async (values: ProfileFormValues) => {\n const nextEmail = values.email?.trim() ?? ''\n const currentPassword = values.currentPassword?.trim() ?? ''\n const password = values.password?.trim() ?? ''\n\n if (!password && nextEmail === email) {\n throw createCrudFormError(t('auth.profile.form.errors.noChanges', 'No changes to save.'))\n }\n\n const payload: { email: string; currentPassword?: string; password?: string } = { email: nextEmail }\n if (password) payload.password = password\n if (password) payload.currentPassword = currentPassword\n\n const result = await readApiResultOrThrow<ProfileUpdateResponse>(\n '/api/auth/profile',\n {\n method: 'PUT',\n headers: { 'content-type': 'application/json' },\n body: JSON.stringify(payload),\n },\n { errorMessage: t('auth.profile.form.errors.save', 'Failed to update profile.') },\n )\n\n const resolvedEmail = typeof result?.email === 'string' ? result.email : nextEmail\n setEmail(resolvedEmail)\n setFormKey((prev) => prev + 1)\n flash(t('auth.profile.form.success', 'Profile updated.'), 'success')\n router.refresh()\n }, [email, router, t])\n\n if (loading) {\n return <LoadingMessage label={t('auth.profile.form.loading', 'Loading profile...')} />\n }\n\n if (error) {\n return <ErrorMessage label={error} />\n }\n\n return (\n <section className=\"space-y-6 rounded-lg border bg-background p-6 max-w-2xl\">\n <header className=\"flex flex-col gap-4 sm:flex-row sm:items-start sm:justify-between\">\n <div className=\"space-y-1\">\n <h2 className=\"text-lg font-semibold\">{t('auth.changePassword.title', 'Change Password')}</h2>\n <p className=\"text-sm text-muted-foreground\">\n {t('auth.profile.subtitle', 'Change password')}\n </p>\n </div>\n <Button type=\"submit\" form={formId}>\n <Save className=\"size-4 mr-2\" />\n {t('auth.profile.form.save', 'Save changes')}\n </Button>\n </header>\n <CrudForm<ProfileFormValues>\n key={formKey}\n formId={formId}\n schema={schema}\n fields={fields}\n initialValues={{\n email,\n currentPassword: '',\n password: '',\n confirmPassword: '',\n }}\n submitLabel={t('auth.profile.form.save', 'Save changes')}\n onSubmit={handleSubmit}\n embedded\n hideFooterActions\n />\n </section>\n )\n}\n"],
5
+ "mappings": ";AAyKW,cAUH,YAVG;AAxKX,YAAY,WAAW;AACvB,SAAS,iBAAiB;AAC1B,SAAS,SAAS;AAClB,SAAS,YAAY;AACrB,SAAS,gBAAgC;AACzC,SAAS,SAAS,4BAA4B;AAC9C,SAAS,2BAA2B;AACpC,SAAS,aAAa;AACtB,SAAS,gBAAgB,oBAAoB;AAC7C,SAAS,cAAc;AACvB,SAAS,YAAY;AACrB,SAAS,qBAAqB,4BAA4B,yBAAyB;AAkBpE,SAAR,4BAA6C;AAClD,QAAM,IAAI,KAAK;AACf,QAAM,SAAS,UAAU;AACzB,QAAM,CAAC,SAAS,UAAU,IAAI,MAAM,SAAS,IAAI;AACjD,QAAM,CAAC,OAAO,QAAQ,IAAI,MAAM,SAAwB,IAAI;AAC5D,QAAM,CAAC,OAAO,QAAQ,IAAI,MAAM,SAAS,EAAE;AAC3C,QAAM,CAAC,SAAS,UAAU,IAAI,MAAM,SAAS,CAAC;AAC9C,QAAM,SAAS,MAAM,MAAM;AAC3B,QAAM,iBAAiB,MAAM,QAAQ,MAAM,kBAAkB,GAAG,CAAC,CAAC;AAClE,QAAM,uBAAuB,MAAM;AAAA,IACjC,MAAM,2BAA2B,gBAAgB,CAAC;AAAA,IAClD,CAAC,gBAAgB,CAAC;AAAA,EACpB;AACA,QAAM,sBAAsB,MAAM,QAAQ,MACxC,uBACI,EAAE,mCAAmC,yCAAyC,EAAE,cAAc,qBAAqB,CAAC,IACpH,QACH,CAAC,sBAAsB,CAAC,CAAC;AAE5B,QAAM,UAAU,MAAM;AACpB,QAAI,YAAY;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AU,iCACV,YAAE,yBAAyB,iBAAiB,GAC/C;AAAA,SACF;AAAA,MACA,qBAAC,UAAO,MAAK,UAAS,MAAM,QAC1B;AAAA,4BAAC,QAAK,WAAU,eAAc;AAAA,QAC7B,EAAE,0BAA0B,cAAc;AAAA,SAC7C;AAAA,OACF;AAAA,IACA;AAAA,MAAC;AAAA;AAAA,QAEC;AAAA,QACA;AAAA,QACA;AAAA,QACA,eAAe;AAAA,UACb;AAAA,UACA,iBAAiB;AAAA,UACjB,UAAU;AAAA,UACV,iBAAiB;AAAA,QACnB;AAAA,QACA,aAAa,EAAE,0BAA0B,cAAc;AAAA,QACvD,UAAU;AAAA,QACV,UAAQ;AAAA,QACR,mBAAiB;AAAA;AAAA,MAbZ;AAAA,IAcP;AAAA,KACF;AAEJ;",
6
6
  "names": []
7
7
  }
@@ -1,6 +1,6 @@
1
1
  "use client";
2
2
  import { createLocalId } from "./productForm.js";
3
- import { parseNumericInput } from "./productFormUtils.js";
3
+ import { isCatalogPriceAmountInputValid } from "../../lib/priceValidation.js";
4
4
  const VARIANT_BASE_VALUES = {
5
5
  name: "",
6
6
  sku: "",
@@ -56,8 +56,7 @@ function findInvalidVariantPriceKinds(priceKinds, priceDrafts) {
56
56
  const draft = priceDrafts?.[kind.id];
57
57
  const amount = typeof draft?.amount === "string" ? draft.amount.trim() : "";
58
58
  if (!amount) continue;
59
- const numeric = parseNumericInput(amount);
60
- if (!Number.isFinite(numeric) || numeric < 0) invalid.push(kind.id);
59
+ if (!isCatalogPriceAmountInputValid(amount)) invalid.push(kind.id);
61
60
  }
62
61
  return invalid;
63
62
  }
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../../../../src/modules/catalog/components/products/variantForm.ts"],
4
- "sourcesContent": ["\"use client\"\n\nimport type { ProductMediaItem } from './ProductMediaManager'\nimport { createLocalId, type PriceKindSummary } from './productForm'\nimport { parseNumericInput } from './productFormUtils'\n\nexport type OptionDefinition = {\n id: string\n code: string\n label: string\n values: Array<{ id: string; label: string }>\n}\n\nexport type VariantPriceDraft = {\n priceKindId: string\n priceId?: string\n amount: string\n currencyCode?: string | null\n displayMode: 'including-tax' | 'excluding-tax'\n}\n\nexport type VariantFormValues = {\n name: string\n sku: string\n barcode: string\n isDefault: boolean\n isActive: boolean\n optionValues: Record<string, string>\n metadata?: Record<string, unknown> | null\n mediaDraftId: string\n mediaItems: ProductMediaItem[]\n defaultMediaId: string | null\n defaultMediaUrl: string\n prices: Record<string, VariantPriceDraft>\n taxRateId: string | null\n customFieldsetCode?: string | null\n}\n\nexport const VARIANT_BASE_VALUES: VariantFormValues = {\n name: '',\n sku: '',\n barcode: '',\n isDefault: false,\n isActive: true,\n optionValues: {},\n metadata: {},\n mediaDraftId: '',\n mediaItems: [],\n defaultMediaId: null,\n defaultMediaUrl: '',\n prices: {},\n taxRateId: null,\n customFieldsetCode: null,\n}\n\nexport const createVariantInitialValues = (): VariantFormValues => ({\n ...VARIANT_BASE_VALUES,\n mediaDraftId: createLocalId(),\n})\n\nexport function normalizeOptionSchema(raw: unknown): OptionDefinition[] {\n if (!Array.isArray(raw)) return []\n return raw\n .map((entry) => normalizeOptionDefinition(entry))\n .filter((entry): entry is OptionDefinition => !!entry)\n}\n\nfunction normalizeOptionDefinition(entry: unknown): OptionDefinition | null {\n if (!entry || typeof entry !== 'object') return null\n const code = extractString((entry as any).code) || createLocalId()\n const label = extractString((entry as any).label) || code\n const values = Array.isArray((entry as any).values)\n ? (entry as any).values\n .map((value: any) => {\n const id = extractString(value?.id) || createLocalId()\n const valueLabel = extractString(value?.label) || id\n return { id, label: valueLabel }\n })\n .filter(\n (value: { id: string; label: string }): value is { id: string; label: string } =>\n value.label.length > 0,\n )\n : []\n return {\n id: extractString((entry as any).id) || createLocalId(),\n code,\n label,\n values,\n }\n}\n\nfunction extractString(value: unknown): string {\n return typeof value === 'string' ? value.trim() : ''\n}\n\nexport function buildVariantMetadata(values: VariantFormValues): Record<string, unknown> {\n const metadata = typeof values.metadata === 'object' && values.metadata ? { ...values.metadata } : {}\n return metadata\n}\n\nexport function findInvalidVariantPriceKinds(\n priceKinds: PriceKindSummary[],\n priceDrafts: Record<string, VariantPriceDraft> | undefined,\n): string[] {\n const invalid: string[] = []\n for (const kind of priceKinds) {\n const draft = priceDrafts?.[kind.id]\n const amount = typeof draft?.amount === 'string' ? draft.amount.trim() : ''\n if (!amount) continue\n const numeric = parseNumericInput(amount)\n if (!Number.isFinite(numeric) || numeric < 0) invalid.push(kind.id)\n }\n return invalid\n}\n\nexport function mapPriceItemToDraft(\n item: Record<string, unknown>,\n kindDisplayModes: Map<string, 'including-tax' | 'excluding-tax'>,\n): VariantPriceDraft | null {\n const kindId =\n typeof item.price_kind_id === 'string'\n ? item.price_kind_id\n : typeof item.priceKindId === 'string'\n ? item.priceKindId\n : null\n if (!kindId) return null\n const unitNet =\n typeof item.unit_price_net === 'string'\n ? item.unit_price_net\n : typeof item.unitPriceNet === 'string'\n ? item.unitPriceNet\n : null\n const unitGross =\n typeof item.unit_price_gross === 'string'\n ? item.unit_price_gross\n : typeof item.unitPriceGross === 'string'\n ? item.unitPriceGross\n : null\n const kindMode = kindDisplayModes.get(kindId) ?? (unitGross ? 'including-tax' : 'excluding-tax')\n return {\n priceKindId: kindId,\n priceId: typeof item.id === 'string' ? item.id : undefined,\n amount: kindMode === 'including-tax' ? (unitGross ?? unitNet ?? '') : (unitNet ?? unitGross ?? ''),\n currencyCode:\n typeof item.currency_code === 'string'\n ? item.currency_code\n : typeof item.currencyCode === 'string'\n ? item.currencyCode\n : null,\n displayMode: kindMode,\n }\n}\n"],
5
- "mappings": ";AAGA,SAAS,qBAA4C;AACrD,SAAS,yBAAyB;AAkC3B,MAAM,sBAAyC;AAAA,EACpD,MAAM;AAAA,EACN,KAAK;AAAA,EACL,SAAS;AAAA,EACT,WAAW;AAAA,EACX,UAAU;AAAA,EACV,cAAc,CAAC;AAAA,EACf,UAAU,CAAC;AAAA,EACX,cAAc;AAAA,EACd,Y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AAQ,YAAY;AAC7B,UAAM,QAAQ,cAAc,KAAK,EAAE;AACnC,UAAM,SAAS,OAAO,OAAO,WAAW,WAAW,MAAM,OAAO,KAAK,IAAI;AACzE,QAAI,CAAC,OAAQ;AACb,UAAM,UAAU,kBAAkB,MAAM;AACxC,QAAI,CAAC,OAAO,SAAS,OAAO,KAAK,UAAU,EAAG,SAAQ,KAAK,KAAK,EAAE;AAAA,EACpE;AACA,SAAO;AACT;AAEO,SAAS,oBACd,MACA,kBAC0B;AAC1B,QAAM,SACJ,OAAO,KAAK,kBAAkB,WAC1B,KAAK,gBACL,OAAO,KAAK,gBAAgB,WAC1B,KAAK,cACL;AACR,MAAI,CAAC,OAAQ,QAAO;AACpB,QAAM,UACJ,OAAO,KAAK,mBAAmB,WAC3B,KAAK,iBACL,OAAO,KAAK,iBAAiB,WAC3B,KAAK,eACL;AACR,QAAM,YACJ,OAAO,KAAK,qBAAqB,WAC7B,KAAK,mBACL,OAAO,KAAK,mBAAmB,WAC7B,KAAK,iBACL;AACR,QAAM,WAAW,iBAAiB,IAAI,MAAM,MAAM,YAAY,kBAAkB;AAChF,SAAO;AAAA,IACL,aAAa;AAAA,IACb,SAAS,OAAO,KAAK,OAAO,WAAW,KAAK,KAAK;AAAA,IACjD,QAAQ,aAAa,kBAAmB,aAAa,WAAW,KAAO,WAAW,aAAa;AAAA,IAC/F,cACE,OAAO,KAAK,kBAAkB,WAC1B,KAAK,gBACL,OAAO,KAAK,iBAAiB,WAC3B,KAAK,eACL;AAAA,IACR,aAAa;AAAA,EACf;AACF;",
4
+ "sourcesContent": ["\"use client\"\n\nimport type { ProductMediaItem } from './ProductMediaManager'\nimport { createLocalId, type PriceKindSummary } from './productForm'\nimport { isCatalogPriceAmountInputValid } from '../../lib/priceValidation'\n\nexport type OptionDefinition = {\n id: string\n code: string\n label: string\n values: Array<{ id: string; label: string }>\n}\n\nexport type VariantPriceDraft = {\n priceKindId: string\n priceId?: string\n amount: string\n currencyCode?: string | null\n displayMode: 'including-tax' | 'excluding-tax'\n}\n\nexport type VariantFormValues = {\n name: string\n sku: string\n barcode: string\n isDefault: boolean\n isActive: boolean\n optionValues: Record<string, string>\n metadata?: Record<string, unknown> | null\n mediaDraftId: string\n mediaItems: ProductMediaItem[]\n defaultMediaId: string | null\n defaultMediaUrl: string\n prices: Record<string, VariantPriceDraft>\n taxRateId: string | null\n customFieldsetCode?: string | null\n}\n\nexport const VARIANT_BASE_VALUES: VariantFormValues = {\n name: '',\n sku: '',\n barcode: '',\n isDefault: false,\n isActive: true,\n optionValues: {},\n metadata: {},\n mediaDraftId: '',\n mediaItems: [],\n defaultMediaId: null,\n defaultMediaUrl: '',\n prices: {},\n taxRateId: null,\n customFieldsetCode: null,\n}\n\nexport const createVariantInitialValues = (): VariantFormValues => ({\n ...VARIANT_BASE_VALUES,\n mediaDraftId: createLocalId(),\n})\n\nexport function normalizeOptionSchema(raw: unknown): OptionDefinition[] {\n if (!Array.isArray(raw)) return []\n return raw\n .map((entry) => normalizeOptionDefinition(entry))\n .filter((entry): entry is OptionDefinition => !!entry)\n}\n\nfunction normalizeOptionDefinition(entry: unknown): OptionDefinition | null {\n if (!entry || typeof entry !== 'object') return null\n const code = extractString((entry as any).code) || createLocalId()\n const label = extractString((entry as any).label) || code\n const values = Array.isArray((entry as any).values)\n ? (entry as any).values\n .map((value: any) => {\n const id = extractString(value?.id) || createLocalId()\n const valueLabel = extractString(value?.label) || id\n return { id, label: valueLabel }\n })\n .filter(\n (value: { id: string; label: string }): value is { id: string; label: string } =>\n value.label.length > 0,\n )\n : []\n return {\n id: extractString((entry as any).id) || createLocalId(),\n code,\n label,\n values,\n }\n}\n\nfunction extractString(value: unknown): string {\n return typeof value === 'string' ? value.trim() : ''\n}\n\nexport function buildVariantMetadata(values: VariantFormValues): Record<string, unknown> {\n const metadata = typeof values.metadata === 'object' && values.metadata ? { ...values.metadata } : {}\n return metadata\n}\n\nexport function findInvalidVariantPriceKinds(\n priceKinds: PriceKindSummary[],\n priceDrafts: Record<string, VariantPriceDraft> | undefined,\n): string[] {\n const invalid: string[] = []\n for (const kind of priceKinds) {\n const draft = priceDrafts?.[kind.id]\n const amount = typeof draft?.amount === 'string' ? draft.amount.trim() : ''\n if (!amount) continue\n if (!isCatalogPriceAmountInputValid(amount)) invalid.push(kind.id)\n }\n return invalid\n}\n\nexport function mapPriceItemToDraft(\n item: Record<string, unknown>,\n kindDisplayModes: Map<string, 'including-tax' | 'excluding-tax'>,\n): VariantPriceDraft | null {\n const kindId =\n typeof item.price_kind_id === 'string'\n ? item.price_kind_id\n : typeof item.priceKindId === 'string'\n ? item.priceKindId\n : null\n if (!kindId) return null\n const unitNet =\n typeof item.unit_price_net === 'string'\n ? item.unit_price_net\n : typeof item.unitPriceNet === 'string'\n ? item.unitPriceNet\n : null\n const unitGross =\n typeof item.unit_price_gross === 'string'\n ? item.unit_price_gross\n : typeof item.unitPriceGross === 'string'\n ? item.unitPriceGross\n : null\n const kindMode = kindDisplayModes.get(kindId) ?? (unitGross ? 'including-tax' : 'excluding-tax')\n return {\n priceKindId: kindId,\n priceId: typeof item.id === 'string' ? item.id : undefined,\n amount: kindMode === 'including-tax' ? (unitGross ?? unitNet ?? '') : (unitNet ?? unitGross ?? ''),\n currencyCode:\n typeof item.currency_code === 'string'\n ? item.currency_code\n : typeof item.currencyCode === 'string'\n ? item.currencyCode\n : null,\n displayMode: kindMode,\n }\n}\n"],
5
+ "mappings": ";AAGA,SAAS,qBAA4C;AACrD,SAAS,sCAAsC;AAkCxC,MAAM,sBAAyC;AAAA,EACpD,MAAM;AAAA,EACN,KAAK;AAAA,EACL,SAAS;AAAA,EACT,WAAW;AAAA,EACX,UAAU;AAAA,EACV,cAAc,CAAC;AAAA,EACf,UAAU,CAAC;AAAA,EACX,cAAc;AAAA,EACd,YAAY,CAAC;AAAA,EACb,gBAAgB;AAAA,EAChB,iBAAiB;AAAA,EACjB,QAAQ,CAAC;AAAA,EACT,WAAW;AAAA,EACX,oBAAoB;AACtB;AAEO,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AAI,CAAC,OAAQ;AACb,QAAI,CAAC,+BAA+B,MAAM,EAAG,SAAQ,KAAK,KAAK,EAAE;AAAA,EACnE;AACA,SAAO;AACT;AAEO,SAAS,oBACd,MACA,kBAC0B;AAC1B,QAAM,SACJ,OAAO,KAAK,kBAAkB,WAC1B,KAAK,gBACL,OAAO,KAAK,gBAAgB,WAC1B,KAAK,cACL;AACR,MAAI,CAAC,OAAQ,QAAO;AACpB,QAAM,UACJ,OAAO,KAAK,mBAAmB,WAC3B,KAAK,iBACL,OAAO,KAAK,iBAAiB,WAC3B,KAAK,eACL;AACR,QAAM,YACJ,OAAO,KAAK,qBAAqB,WAC7B,KAAK,mBACL,OAAO,KAAK,mBAAmB,WAC7B,KAAK,iBACL;AACR,QAAM,WAAW,iBAAiB,IAAI,MAAM,MAAM,YAAY,kBAAkB;AAChF,SAAO;AAAA,IACL,aAAa;AAAA,IACb,SAAS,OAAO,KAAK,OAAO,WAAW,KAAK,KAAK;AAAA,IACjD,QAAQ,aAAa,kBAAmB,aAAa,WAAW,KAAO,WAAW,aAAa;AAAA,IAC/F,cACE,OAAO,KAAK,kBAAkB,WAC1B,KAAK,gBACL,OAAO,KAAK,iBAAiB,WAC3B,KAAK,eACL;AAAA,IACR,aAAa;AAAA,EACf;AACF;",
6
6
  "names": []
7
7
  }
@@ -1,6 +1,10 @@
1
1
  import { z } from "zod";
2
2
  import { CATALOG_PRICE_DISPLAY_MODES, CATALOG_PRODUCT_TYPES } from "./types.js";
3
3
  import { REFERENCE_UNIT_CODES } from "../lib/unitCodes.js";
4
+ import {
5
+ getCatalogPriceAmountValidationMessage,
6
+ validateCatalogPriceAmountInput
7
+ } from "../lib/priceValidation.js";
4
8
  const uuid = () => z.string().uuid();
5
9
  const scoped = z.object({
6
10
  organizationId: uuid(),
@@ -70,6 +74,15 @@ const unitPriceConfigSchema = z.object({
70
74
  referenceUnit: unitPriceReferenceUnitSchema.nullable().optional(),
71
75
  baseQuantity: z.coerce.number().positive().optional()
72
76
  });
77
+ const catalogPriceAmountSchema = z.custom((value) => validateCatalogPriceAmountInput(value).ok, {
78
+ message: getCatalogPriceAmountValidationMessage()
79
+ }).transform((value) => {
80
+ const result = validateCatalogPriceAmountInput(value);
81
+ if (!result.ok) {
82
+ throw new Error("catalogPriceAmountSchema transform reached invalid state");
83
+ }
84
+ return result.numeric;
85
+ });
73
86
  function productUomCrossFieldRefinement(input, ctx) {
74
87
  const defaultUnit = typeof input.defaultUnit === "string" ? input.defaultUnit.trim() : "";
75
88
  const defaultSalesUnit = typeof input.defaultSalesUnit === "string" ? input.defaultSalesUnit.trim() : "";
@@ -203,8 +216,8 @@ const priceCreateSchema = scoped.extend({
203
216
  priceKindId: uuid(),
204
217
  minQuantity: z.coerce.number().int().min(1).optional(),
205
218
  maxQuantity: z.coerce.number().int().min(1).optional(),
206
- unitPriceNet: z.coerce.number().min(0).optional(),
207
- unitPriceGross: z.coerce.number().min(0).optional(),
219
+ unitPriceNet: catalogPriceAmountSchema.optional(),
220
+ unitPriceGross: catalogPriceAmountSchema.optional(),
208
221
  taxRate: z.coerce.number().min(0).max(100).optional(),
209
222
  taxRateId: uuid().nullable().optional(),
210
223
  channelId: uuid().optional(),