Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
355f21b
Passwords also refuse a distinctive word of an institute's name, not …
omerFixesBugs Nov 16, 2026
7016eff
Sprint 1 P1-J plan (docs/plans/sprint-1-p1j-password-hash.md): the ap…
omerFixesBugs Nov 16, 2026
25da8a3
Sprint 1 P2 plan (docs/plans/sprint-1-p2-files.md): institute profile…
omerFixesBugs Nov 16, 2026
995e61e
Test users get phone numbers no other test user of the same database …
omerFixesBugs Nov 16, 2026
356c448
The app login can no longer read users.password_hash (P1-J): SELECT o…
omerFixesBugs Nov 16, 2026
3ca69eb
Every Prisma client leaves users.password_hash out of its reads (P1-J…
omerFixesBugs Nov 16, 2026
30e85f6
Files and the institute profile in the database (P2 task 1): stored_f…
omerFixesBugs Nov 16, 2026
687a83b
CORS_ORIGINS is normalised in env.ts as browsers send Origin (lower-c…
omerFixesBugs Nov 16, 2026
ff8981c
Shared file caps, types and the institute profile schema (P2 task 2):…
omerFixesBugs Nov 16, 2026
917d5eb
The start-up login check also refuses a login with REPLICATION (a rep…
omerFixesBugs Nov 16, 2026
bc6644b
Password login without the hash (P1-J): the API hashes the typed pass…
omerFixesBugs Nov 16, 2026
b0e6315
GET /me/password reads hasPassword from auth_my_password_is_set() (P1…
omerFixesBugs Nov 16, 2026
47fb357
Docs for P1-J: database.md (password_hash not readable by the app, a …
omerFixesBugs Nov 16, 2026
20b29e9
Redis is pinned to one exact version, redis:7.4.11-alpine (the versio…
omerFixesBugs Nov 16, 2026
81bfc26
Docs for the follow-ups: decisions-pending gets the section "Small fo…
omerFixesBugs Nov 16, 2026
9cc9d54
Image processing for uploads (P2 task 3): sharp 0.35.5 (already in th…
omerFixesBugs Nov 16, 2026
b0e010b
FileStore and the per-institute cap (P2 task 4): PostgresFileStore is…
omerFixesBugs Nov 16, 2026
caec9f7
Photo and profile routes (P2 task 5): GET/PUT/DELETE /students/:id/ph…
omerFixesBugs Nov 16, 2026
57cf79f
Web (P2 task 6): Settings → Institute profile (names, EIIN, contact, …
omerFixesBugs Nov 16, 2026
4e4cbb2
Docs for P2 (task 7): docs/files.md, database.md (the three tables an…
omerFixesBugs Nov 16, 2026
724ffb0
The photo dialog's preview URL is made when a file is chosen, not in …
omerFixesBugs Nov 16, 2026
110857d
files.spec: the profile fields by id ("নাম (বাংলা)" is also part of t…
omerFixesBugs Nov 16, 2026
8c3a4f9
After the review (P1-J7): a dedicated login for the password login. j…
omerFixesBugs Nov 16, 2026
72b714b
The password login looks the hash up on its own database login (Login…
omerFixesBugs Nov 16, 2026
06f3236
Docs after the P1-J review: what an injection in an ordinary request …
omerFixesBugs Nov 16, 2026
82f6600
Test phones never wrap and their lock survives races (review of PR #1…
omerFixesBugs Nov 16, 2026
27403c3
The institute-name rule compares passwords and names like the phone r…
omerFixesBugs Nov 16, 2026
7aa31c5
An origin setting with a login, a path, a query or a fragment is no o…
omerFixesBugs Nov 16, 2026
fd6430e
Docs after the review of PR #17: P1-I's title line says it is built h…
omerFixesBugs Nov 16, 2026
9dbee45
P1-J migration after the second review: it first checks that jantrik_…
omerFixesBugs Nov 16, 2026
a980a60
The password login's database login must reach the same database, por…
omerFixesBugs Nov 16, 2026
ff673b8
Docs after the second P1-J review: the server upgrade creates the rol…
omerFixesBugs Nov 16, 2026
13deed2
A file reference is tied to its kind (review R6): UNIQUE (tenant_id, …
omerFixesBugs Nov 16, 2026
cb86722
Uploads hold no database connection while they arrive (review R1): th…
omerFixesBugs Nov 16, 2026
459bcf7
Web after the review: a chosen image is shrunk in the browser to 1600…
omerFixesBugs Nov 16, 2026
944e840
Docs after the review: auth.md (the request pipeline and @OwnTransact…
omerFixesBugs Nov 16, 2026
282c8b6
Review round 2, API: uploads in flight bounded before multer (16 per …
omerFixesBugs Nov 16, 2026
cfd4506
Review round 2, web: the browser asks for the EXIF orientation explic…
omerFixesBugs Nov 16, 2026
73eb83b
Docs for review round 2: auth.md (the upload routes' re-checks, Uploa…
omerFixesBugs Nov 16, 2026
e622e52
The interceptor opens its transaction through an unchecked entry (aro…
omerFixesBugs Nov 16, 2026
3d2c5d5
Review round 3: no upload slot leaks when the client left before Uplo…
omerFixesBugs Nov 16, 2026
de19108
The two new origin tests' development settings also name DATABASE_LOG…
omerFixesBugs Nov 16, 2026
aa0105b
Merge P1-J (#16) into the small follow-ups (#17): the ready resolutio…
omerFixesBugs Nov 16, 2026
b1efceb
Merge remote-tracking branch 'origin/feature/sprint-1-p1j-password-ha…
omerFixesBugs Nov 16, 2026
b5d3697
Merge remote-tracking branch 'origin/feature/sprint-1-p2-files' into …
omerFixesBugs Nov 16, 2026
c857102
Merge remote-tracking branch 'origin/feature/small-followups-after-p1…
omerFixesBugs Nov 16, 2026
9309fa8
Work in progress: a staff guide draft and draft legal texts (terms of…
omerFixesBugs Nov 19, 2026
ed5d56c
Docs only: a plain staff guide draft (signing in, setup, students, gu…
omerFixesBugs Nov 19, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Shared file caps, types and the institute profile schema (P2 task 2):…
… FILE_LIMITS per kind equal to the database CHECKs, the 5 MB upload and 50 megapixel limits, FileRef and InstituteProfile; the schema accepts Bangla digits (EIIN, phone, year), stores a mobile as E.164 and a landline as digits, and adds https:// to a bare website

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
  • Loading branch information
omerFixesBugs and claude committed Nov 16, 2026
commit ff8981cb1d1c9e618a72c1c852bca6bf8f9fe088
86 changes: 86 additions & 0 deletions packages/shared/src/files-schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { z } from 'zod';
import { toAsciiDigits } from './digits.js';
import { INSTITUTE_PROFILE_LIMITS as L } from './files.js';
import { normalizeBdPhone } from './phone.js';

// PUT /institute/profile (sprint 1 P2). Each rule matches a CHECK on institute_profiles, so a
// value the schema accepts is never refused by the database.

const text = (max: number) =>
z
.string()
.trim()
.max(max)
.nullish()
.transform((v) => v || null);

/** Bangla digits and spaces accepted; empty → null. */
const digitsText = z
.string()
.nullish()
.transform((v) => (v ? toAsciiDigits(v).replace(/\s/g, '') : '') || null);

/** A mobile is stored as E.164; anything else as its digits (a landline), with an optional +. */
export function normalizeInstitutePhone(input: string): string {
return (
normalizeBdPhone(input) ??
toAsciiDigits(input)
.trim()
.replace(/[\s\-().]/g, '')
);
}

/** `shapla.edu.bd` → `https://shapla.edu.bd`; a given http(s) scheme is kept. */
export function normalizeWebsite(input: string): string {
const v = input.trim();
return /^[a-z][a-z0-9+.-]*:/i.test(v) ? v : `https://${v}`;
}

export const instituteProfileSchema = z
.object({
nameBn: text(L.name),
nameEn: text(L.name),
shortName: text(L.shortName),
eiin: digitsText.refine((v) => v === null || /^\d{6}$/.test(v), 'EIIN is 6 digits'),
addressBn: text(L.address),
addressEn: text(L.address),
phone: z
.string()
.nullish()
.transform((v) => (v?.trim() ? normalizeInstitutePhone(v) : null))
.refine((v) => v === null || /^\+?\d{5,15}$/.test(v), 'A phone number of 5-15 digits'),
email: text(L.email).refine(
(v) => v === null || /^[^@\s]+@[^@\s]+\.[^@\s]+$/.test(v),
'An e-mail address',
),
website: z
.string()
.nullish()
.transform((v) => (v?.trim() ? normalizeWebsite(v) : null))
.refine(
(v) => v === null || (v.length <= L.website && /^https?:\/\/\S+$/i.test(v)),
'A web address (http or https)',
),
headTitleBn: text(L.headTitle),
headTitleEn: text(L.headTitle),
headNameBn: text(L.headName),
headNameEn: text(L.headName),
establishedYear: z
.union([z.number(), z.string()])
.nullish()
.transform((v) => {
if (v === null || v === undefined) return null;
const s = toAsciiDigits(String(v)).trim();
return s === '' ? null : Number(s);
})
.refine(
(v) => v === null || (Number.isInteger(v) && v >= L.yearMin && v <= L.yearMax),
'A year between 1800 and 2100',
),
})
.refine((p) => p.nameBn !== null || p.nameEn !== null, {
message: 'A name in Bangla or English is required',
path: ['nameBn'],
});

export type InstituteProfileInput = z.input<typeof instituteProfileSchema>;
83 changes: 83 additions & 0 deletions packages/shared/src/files.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { describe, expect, it } from 'vitest';
import { FILE_LIMITS, STORED_FILE_KINDS, UPLOAD_MAX_BYTES } from './files.js';
import {
instituteProfileSchema,
normalizeInstitutePhone,
normalizeWebsite,
} from './files-schema.js';

describe('file limits (docs/files.md, the stored_files CHECKs)', () => {
it('has a limit for every kind, equal to the database caps', () => {
expect(Object.keys(FILE_LIMITS).sort()).toEqual([...STORED_FILE_KINDS].sort());
expect(FILE_LIMITS.STUDENT_PHOTO.maxBytes).toBe(204_800);
expect(FILE_LIMITS.APPLICANT_PHOTO.maxBytes).toBe(204_800);
expect(FILE_LIMITS.LOGO.maxBytes).toBe(102_400);
expect(FILE_LIMITS.SEAL.maxBytes).toBe(102_400);
expect(FILE_LIMITS.SIGNATURE.maxBytes).toBe(102_400);
expect(FILE_LIMITS.DOCUMENT_ASSET.maxBytes).toBe(262_144);
expect(FILE_LIMITS.STUDENT_PHOTO.mimes).toEqual(['image/jpeg']);
expect(FILE_LIMITS.SIGNATURE.mimes).toEqual(['image/png']);
expect(UPLOAD_MAX_BYTES).toBe(5 * 1024 * 1024);
});
});

describe('instituteProfileSchema', () => {
const parse = (v: Record<string, unknown>) =>
instituteProfileSchema.safeParse({ nameBn: 'শাপলা', ...v });

it('trims, turns empty strings into null, and needs one name', () => {
const r = instituteProfileSchema.parse({
nameBn: ' শাপলা উচ্চ বিদ্যালয় ',
nameEn: '',
eiin: '',
});
expect(r).toMatchObject({
nameBn: 'শাপলা উচ্চ বিদ্যালয়',
nameEn: null,
eiin: null,
phone: null,
});
expect(instituteProfileSchema.safeParse({ nameBn: ' ', nameEn: '' }).success).toBe(false);
});

it('accepts Bangla digits in the EIIN, the phone and the year', () => {
const r = instituteProfileSchema.parse({
nameEn: 'Shapla',
eiin: '১০৮ ১২৩',
phone: '০১৭১১-০০০০০০',
establishedYear: '১৯৬৫',
});
expect(r).toMatchObject({ eiin: '108123', phone: '+8801711000000', establishedYear: 1965 });
});

it.each([
['01711000000', '+8801711000000'],
['+880 1711-000000', '+8801711000000'],
['02-9123456', '029123456'],
['(031) 712 345', '031712345'],
])('stores the phone %s as %s', (typed, stored) => {
expect(normalizeInstitutePhone(typed)).toBe(stored);
expect(parse({ phone: typed }).success).toBe(true);
});

it.each([
['a 5-digit EIIN', { eiin: '12345' }],
['letters in the EIIN', { eiin: '1081a3' }],
['a 4-digit phone', { phone: '1234' }],
['letters in the phone', { phone: 'call me' }],
['an e-mail without a domain', { email: 'office@' }],
['an ftp website', { website: 'ftp://shapla.edu.bd' }],
['year 1700', { establishedYear: 1700 }],
['year 2101', { establishedYear: '2101' }],
['a 31-character short name', { shortName: 'x'.repeat(31) }],
['a 201-character name', { nameBn: 'ক'.repeat(201) }],
])('refuses %s (as the database CHECK would)', (_name, v) => {
expect(parse(v).success).toBe(false);
});

it('adds https:// to a bare website and keeps a given scheme', () => {
expect(normalizeWebsite('shapla.edu.bd')).toBe('https://shapla.edu.bd');
expect(normalizeWebsite('http://shapla.edu.bd')).toBe('http://shapla.edu.bd');
expect(parse({ website: 'shapla.edu.bd' }).data?.website).toBe('https://shapla.edu.bd');
});
});
121 changes: 121 additions & 0 deletions packages/shared/src/files.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
// Stored images (sprint 1 P2, docs/files.md): the kinds, their caps and the API's shapes. No zod
// here: the web imports these; the input schema is in files-schema.ts.

export const STORED_FILE_KINDS = [
'STUDENT_PHOTO',
'APPLICANT_PHOTO',
'LOGO',
'SIGNATURE',
'SEAL',
'DOCUMENT_ASSET',
] as const;
export type StoredFileKind = (typeof STORED_FILE_KINDS)[number];

export type StoredMime = 'image/jpeg' | 'image/png' | 'image/webp';

export interface FileLimit {
/** Stored size cap in bytes (the database CHECK stored_files_size has the same numbers). */
maxBytes: number;
/** The box the image is shrunk to fit (never enlarged); null: no resize (P8's assets). */
box: { width: number; height: number } | null;
/** Formats stored for this kind, preferred first (CHECK stored_files_mime). */
mimes: readonly StoredMime[];
}

/** Per kind, what the server stores after re-encoding the upload (decision S7). */
export const FILE_LIMITS: Record<StoredFileKind, FileLimit> = {
STUDENT_PHOTO: { maxBytes: 200 * 1024, box: { width: 600, height: 800 }, mimes: ['image/jpeg'] },
APPLICANT_PHOTO: {
maxBytes: 200 * 1024,
box: { width: 600, height: 800 },
mimes: ['image/jpeg'],
},
LOGO: {
maxBytes: 100 * 1024,
box: { width: 512, height: 512 },
mimes: ['image/png', 'image/webp'],
},
SEAL: {
maxBytes: 100 * 1024,
box: { width: 512, height: 512 },
mimes: ['image/png', 'image/webp'],
},
SIGNATURE: { maxBytes: 100 * 1024, box: { width: 600, height: 240 }, mimes: ['image/png'] },
DOCUMENT_ASSET: {
maxBytes: 256 * 1024,
box: null,
mimes: ['image/jpeg', 'image/png', 'image/webp'],
},
};

/** The largest upload the API reads (multer `fileSize`); the web checks it before sending. */
export const UPLOAD_MAX_BYTES = 5 * 1024 * 1024;
/** Decoded pixels allowed (sharp `limitInputPixels`): a 48 MP phone photo fits, a bomb does not. */
export const UPLOAD_MAX_PIXELS = 50_000_000;
/** What the file picker offers (the server decides by content, not by this). */
export const UPLOAD_ACCEPT = 'image/jpeg,image/png,image/webp';

/** The institute profile's three images, by their route name. */
export const INSTITUTE_IMAGE_SLOTS = ['logo', 'signature', 'seal'] as const;
export type InstituteImageSlot = (typeof INSTITUTE_IMAGE_SLOTS)[number];
export const INSTITUTE_IMAGE_KIND: Record<InstituteImageSlot, StoredFileKind> = {
logo: 'LOGO',
signature: 'SIGNATURE',
seal: 'SEAL',
};

/** A stored image as the API describes it (never its bytes; those come from an entity route). */
export interface FileRef {
/** The stored file's id: also the web's cache key (a replaced image has a new id). */
id: string;
mime: StoredMime;
sizeBytes: number;
width: number;
height: number;
}

/** The text fields of the institute profile (PUT /institute/profile). */
export interface InstituteProfileFields {
nameBn: string | null;
nameEn: string | null;
shortName: string | null;
/** BANBEIS EIIN: 6 ASCII digits. */
eiin: string | null;
addressBn: string | null;
addressEn: string | null;
/** A mobile as E.164, or a landline as ASCII digits (optional leading +). */
phone: string | null;
email: string | null;
website: string | null;
headTitleBn: string | null;
headTitleEn: string | null;
headNameBn: string | null;
headNameEn: string | null;
establishedYear: number | null;
}

/** GET /institute/profile. */
export interface InstituteProfile extends InstituteProfileFields {
/** False until the first save or image: the fields are then empty. */
saved: boolean;
/** The name the platform registered (tenants.name), to pre-fill a new profile. */
registeredName: string;
logo: FileRef | null;
signature: FileRef | null;
seal: FileRef | null;
/** This institute's stored images in total, and its cap (FILE_STORAGE_PER_TENANT_MB). */
storage: { bytesUsed: number; capBytes: number };
}

/** Field limits shared by the schema and the form (the CHECKs on institute_profiles). */
export const INSTITUTE_PROFILE_LIMITS = {
name: 200,
shortName: 30,
address: 300,
headTitle: 60,
headName: 100,
email: 254,
website: 200,
yearMin: 1800,
yearMax: 2100,
} as const;
2 changes: 2 additions & 0 deletions packages/shared/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,5 @@ export * from './staff-attendance-api.js';
export * from './staff-attendance-schema.js';
export * from './password.js';
export * from './password-schema.js';
export * from './files.js';
export * from './files-schema.js';