# Bibhuti Pathashala — Google Sheets setup

## Important: public access
This app intentionally has no login. Anyone who obtains the app or deployed script URL can read school leave records, edit teacher lists, and approve or reject applications. Names entered as decision makers are not verified identities. A hidden spreadsheet or an unlisted URL does not provide access control. Do not use this deployment for confidential information or share it beyond trusted school staff. Add authentication before exposing it more broadly.

## 1. Create the database
1. Create a new Google Spreadsheet in the school's Google account.
2. Open **Extensions → Apps Script** from that spreadsheet.
3. Replace the default `Code.gs` with the supplied `Code.gs` file.
4. In Apps Script **Project Settings**, enable **Show appsscript.json manifest file**.
5. Replace the manifest with the supplied `appsscript.json`.
6. Select `setup` in the function dropdown and click **Run**. Review and grant the spreadsheet permission.
7. Return to the spreadsheet. The script creates **Leave Records**, **Teachers**, **Settings**, and **Activity Log** with the required column headers. It does not create fictional teachers or leave records. The Settings worksheet contains only the school's identity and standard leave/approval categories.

The spreadsheet ID stays in Apps Script's Script Properties. The browser never supplies a spreadsheet ID or sees Google account credentials.

## 2. Deploy the API
1. Click **Deploy → New deployment → Web app**.
2. Set **Execute as** to **Me** (the school account).
3. Set **Who has access** to **Anyone**. This is necessary for the requested no-login app. Workspace policies may prevent this option; if so, your Google Workspace administrator must permit it or you must choose a protected design instead.
4. Click **Deploy**, authorize, and copy the URL ending in `/exec`, not `/dev`.
5. Open the app's **Settings** page, paste the URL, and click **Connect & Test**.
6. The app checks the `bootstrap` action before marking the connection as successful. Add real teachers through Settings. For another browser/device, enter the URL there as well. Only this public connection URL is saved in the browser; all leave and teacher data remains in Google Sheets.

## 3. Verify with real school data
1. Add one real teacher (ID, name, department, designation, Active).
2. Create a leave application. Select the teacher, leave type, valid inclusive dates, and a reason.
3. Review the confirmation and submit. Success appears only after the script saves and reads the record back from Google Sheets.
4. Find the assigned `BP-year-identifier` in **Leave Records** and the Google Sheet.
5. Open the application, enter the decision maker's name, and approve it. Rejecting requires a remark. Check both the row and Activity Log.
6. Refresh, search, export CSV, and print an individual application or a date-range report.
7. Confirm there are no duplicate records after retrying the same submission. The script holds a lock and remembers submission keys in Activity Log.

## 4. Updating the backend
After code changes, choose **Deploy → Manage deployments → Edit → New version → Deploy**. Keep the existing deployment URL. Test the connection again. Changing source alone does not update a deployed version.

## API contract
Read calls use `GET ?action=bootstrap`, `getDashboardData`, `getLeaveRecords`, `getTeachers`, `getSettings`, `getLeaveApplicationById&id=...`, `searchLeaveApplications&filters=...`, or `exportLeaveRecords`.

Write calls use POST with `Content-Type: text/plain;charset=utf-8` and a JSON body `{ "action": "actionName", "data": { ... } }`. This avoids a browser preflight request that Apps Script cannot serve. Do not use `no-cors`: it hides the response and cannot confirm a save.

Write actions: `submitLeaveApplication`, `updateLeaveStatus`, `updateLeaveApplication`, `saveTeacher`, `saveSettings`.

All actions return `{ "success": true, "data": ... }` or `{ "success": false, "error": "..." }`. Client forms retain their values when a request fails. Server validation controls teacher identity, categories, inclusive date ranges, and generated IDs. Application updates replace the existing row, not append a new application. Text cells and CSV output neutralize spreadsheet-formula prefixes. Teacher contact numbers are returned for the selected application document; they are public to anyone with the deployment URL.

## Troubleshooting and limits
- A Google sign-in page instead of JSON means the deployment isn't accessible to Anyone or the school Workspace blocks anonymous access.
- Use `/exec`. `/dev` is editor-only.
- If a browser blocks a cross-origin response, check the deployment's access, redirects, and Google Workspace policy. The app does not use public proxy services and will never claim a save without a readable response.
- Apps Script and Google Sheets have execution and daily quotas. Reads are cached for one minute; use Refresh when you need current data. This is not a streaming database.
- Apps Script/Sheets do not provide multi-sheet transactions. Keep backups and reconcile Activity Log if an execution stops between a record write and audit write.
- The project uses React and Vite within TanStack Start's built-in routing; replacing the supported router is not necessary.
- Connecting and testing the live Google deployment is pending until you deploy and provide the URL. The current disconnected app is a development preview with no fabricated records.

## Official application documents
- Use Settings → School details & preferences to save the school name, address, optional logo, and BS academic year (such as 2083 or 2083/84). Leave the year blank until the school confirms it.
- Logos are resized in the browser and stored as a raster data URL in the existing Settings worksheet (maximum 32,000 characters). Removing a logo takes effect after Save Preferences.
- Application dates remain canonical Gregorian `YYYY-MM-DD` values in Sheets. The app uses `nepali-date-converter` to display BS `YYYY/MM/DD` in applications and prints, without changing leave duration. Unsupported dates show “BS date unavailable”, never a guessed date.
- Open a record → Print Application → review the document → Print / Save PDF. Select A4 portrait and disable browser headers and footers to avoid the browser adding its own date or URL.
- For an existing deployment, replace Code.gs with the updated download and deploy a New version. Existing worksheets and records are preserved; no migration or rerun of setup is needed.
- This app still has no login: Settings is not restricted to administrators. Keep both app and deployment URLs within your trusted school team.
