# Model Layer Guide

This folder holds all MongoDB schemas for the backend.
Each feature has its own folder so the code stays simple and easy to manage.

## How to use

- Import models from `model/index.js`
- Keep every query company-aware in SaaS mode
- Put common helpers in `_shared`

### Example import

```js
const { Employee, LeaveRequest, ApprovalRequest } = require("../model");
```

## Shared helpers

### `_shared/company.plugin.js`

- Adds `companyId` to every SaaS model
- Keeps each company’s data separate

### `_shared/softDelete.plugin.js`

- Adds `isDeleted` and `deletedAt`
- Hides data instead of deleting it forever

## Model sections

### company

#### `company.model.js`

- Stores company or workspace details
- Example: name, slug, status, timezone, currency, owner

### auth

#### `User.model.js`

- Stores login user data and role
- Example: email, password hash, role, active status

### department

#### `Department.model.js`

- Stores department info
- Example: HR, Engineering, Sales

### employee

#### `Employee.model.js`

- Stores employee profile and job data
- Example: employee code, department, designation, salary, status

### attendance

#### `Attendance.model.js`

- Stores daily attendance for one employee
- Example: present, absent, check-in, check-out, overtime

### leave

#### `LeavePolicy.model.js`

- Stores leave rules
- Example: annual quota, paid or unpaid, carry forward

#### `LeaveRequest.model.js`

- Stores leave applications from employees
- Example: start date, end date, reason, approval status

### payroll

#### `PayrollCycle.model.js`

- Stores payroll period info
- Example: month, year, processing status

#### `PayrollRecord.model.js`

- Stores salary result for each employee
- Example: gross, deductions, tax, net salary, payment status

### approval

#### `ApprovalRequest.model.js`

- Stores approval workflow records
- Used for leave, loan, asset, and recruitment requests
- Supports multi-step approval chain

### recruitment

#### `JobOpening.model.js`

- Stores job vacancy details
- Example: title, department, openings, status

#### `Candidate.model.js`

- Stores candidate application data
- Example: name, stage, score, resume

### performance

#### `PerformanceReview.model.js`

- Stores employee review records
- Example: rating, strengths, improvements, goals

### loan

#### `LoanRequest.model.js`

- Stores employee loan requests
- Example: amount, tenure, monthly deduction, status

### asset

#### `Asset.model.js`

- Stores company asset inventory
- Example: laptop, tag, condition, availability

#### `AssetAssignment.model.js`

- Stores asset assignment history
- Example: who got the asset and when it was returned

### announcement

#### `Announcement.model.js`

- Stores internal notices
- Example: all users, one department, or one role

### billing

#### `Subscription.model.js`

- Stores SaaS subscription details
- Example: plan, provider, status, billing period

#### `Invoice.model.js`

- Stores billing invoices
- Example: due amount, paid amount, due date, invoice status

### report

#### `ReportJob.model.js`

- Stores report generation jobs
- Example: report type, format, queued or completed status

## `index.js`

- Exports all models from one place
- Makes imports shorter and cleaner in controllers and services

## Why this structure helps

- Easy to find files
- Easy to maintain
- Easy to grow for SaaS
- Easy to import in controllers/services

## Add a new model

If you add a new feature, create a new folder inside `model` and keep related schemas there.
