@umituz/web-hono 1.7.1 → 1.7.3

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/org.ts +19 -9
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@umituz/web-hono",
3
- "version": "1.7.1",
3
+ "version": "1.7.3",
4
4
  "description": "API backend platform for Cloudflare Workers + Neon Postgres: createApi skeleton + error envelope layer, full-CRUD resource factory (opt-in create/remove, list params), schema→client type bridge (ClientRow), apiFetch/apiWrite client, reactive fetch-backed store (+React hooks), and db lifecycle scripts (reset / snapshot / seed)",
5
5
  "type": "module",
6
6
  "main": "./src/index.ts",
package/src/org.ts CHANGED
@@ -1,10 +1,10 @@
1
- import { eq } from 'drizzle-orm';
1
+ import { and, eq, isNull } from 'drizzle-orm';
2
2
  import type { PgColumn, PgTable } from 'drizzle-orm/pg-core';
3
3
  import type { CrudDb } from './crud';
4
4
 
5
5
  export type UsersLike = PgTable & { id: PgColumn; organizationId: PgColumn };
6
6
  export type OrgsLike = PgTable & { id: PgColumn; modes: PgColumn };
7
- export type NumberedTable = PgTable & { number: PgColumn; organizationId: PgColumn };
7
+ export type NumberedTable = PgTable & { number: PgColumn; organizationId?: PgColumn; deletedAt?: PgColumn };
8
8
 
9
9
  /** Organization of the acting user; missing actor → fallback. */
10
10
  export async function resolveOrg(
@@ -29,19 +29,29 @@ export async function orgHasMode(db: CrudDb, orgs: OrgsLike, orgId: string, mode
29
29
  return Array.isArray(org?.modes) && (org.modes as string[]).includes(mode);
30
30
  }
31
31
 
32
- /** Next `PREFIX-n` document number: max numeric suffix +1. Not a
32
+ /** Next `PREFIX-n` (or `PREFIX-YYYY-NNNN` with year: true) document number:
33
+ * max numeric suffix +1 among the current year's live rows. Not a
33
34
  * reservation — the partial-unique on number turns races into a 409. */
34
35
  export async function nextNumber(
35
36
  db: CrudDb,
36
37
  table: NumberedTable,
37
38
  prefix: string,
38
- opts: { orgId?: string; base?: number } = {},
39
+ opts: { orgId?: string; base?: number; year?: boolean; pad?: number } = {},
39
40
  ): Promise<string> {
40
- const q = db.select({ number: table.number }).from(table);
41
- const rows = opts.orgId ? await q.where(eq(table.organizationId, opts.orgId)) : await q;
41
+ const conds = [];
42
+ if (opts.orgId && table.organizationId) conds.push(eq(table.organizationId, opts.orgId));
43
+ if (table.deletedAt) conds.push(isNull(table.deletedAt));
44
+ const rows = await db
45
+ .select({ number: table.number })
46
+ .from(table)
47
+ .where(conds.length > 0 ? and(...conds) : undefined);
48
+ const withYear = opts.year === true;
49
+ const year = new Date().getFullYear();
50
+ const pat = withYear ? new RegExp(`^${prefix}-${year}-(\\d+)$`) : new RegExp(`^${prefix}-(\\d+)$`);
42
51
  const max = rows.reduce((m, r) => {
43
- const n = new RegExp(`^${prefix}-(\\d+)$`).exec(String(r.number ?? ''))?.[1];
52
+ const n = pat.exec(String(r.number ?? ''))?.[1];
44
53
  return n ? Math.max(m, Number(n)) : m;
45
- }, opts.base ?? 2000);
46
- return `${prefix}-${max + 1}`;
54
+ }, opts.base ?? (withYear ? 0 : 2000));
55
+ const n = max + 1;
56
+ return withYear ? `${prefix}-${year}-${String(n).padStart(opts.pad ?? 4, '0')}` : `${prefix}-${n}`;
47
57
  }