@umituz/web-hono 1.7.1 → 1.7.2
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.
- package/package.json +1 -1
- package/src/org.ts +12 -7
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@umituz/web-hono",
|
|
3
|
-
"version": "1.7.
|
|
3
|
+
"version": "1.7.2",
|
|
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
|
@@ -4,7 +4,7 @@ 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
|
|
7
|
+
export type NumberedTable = PgTable & { number: PgColumn; organizationId?: PgColumn };
|
|
8
8
|
|
|
9
9
|
/** Organization of the acting user; missing actor → fallback. */
|
|
10
10
|
export async function resolveOrg(
|
|
@@ -29,19 +29,24 @@ 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`
|
|
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
41
|
const q = db.select({ number: table.number }).from(table);
|
|
41
|
-
const rows = opts.orgId ? await q.where(eq(table.organizationId, opts.orgId)) : await q;
|
|
42
|
+
const rows = opts.orgId && table.organizationId ? await q.where(eq(table.organizationId, opts.orgId)) : await q;
|
|
43
|
+
const withYear = opts.year === true;
|
|
44
|
+
const year = new Date().getFullYear();
|
|
45
|
+
const pat = withYear ? new RegExp(`^${prefix}-${year}-(\\d+)$`) : new RegExp(`^${prefix}-(\\d+)$`);
|
|
42
46
|
const max = rows.reduce((m, r) => {
|
|
43
|
-
const n =
|
|
47
|
+
const n = pat.exec(String(r.number ?? ''))?.[1];
|
|
44
48
|
return n ? Math.max(m, Number(n)) : m;
|
|
45
|
-
}, opts.base ?? 2000);
|
|
46
|
-
|
|
49
|
+
}, opts.base ?? (withYear ? 0 : 2000));
|
|
50
|
+
const n = max + 1;
|
|
51
|
+
return withYear ? `${prefix}-${year}-${String(n).padStart(opts.pad ?? 4, '0')}` : `${prefix}-${n}`;
|
|
47
52
|
}
|