Factory Workflows
Foot Factory orchestrates complex industrial workflows using a meta-driven approach via TenantConfig, rather than a one-size-fits-all model.
Core Workflows
1. Tenant Provisioning
Triggered by a Super Admin via the admin dashboard or POST /api/v1/tenant.
- Super Admin submits tenant name, slug, plan, and admin credentials.
- API validates the plan slug against the
Plancollection. - A MongoDB transaction atomically creates:
- The
Tenantdocument (withsettingscopied from plan limits) - The admin
Userdocument linked to the tenant - Sets
Tenant.ownerto the new user - On failure, the transaction is rolled back — no partial state.
- After the transaction commits, subdomain provisioning runs concurrently against Vercel and Cloudflare (see Tenant Subdomain Provisioning).
- If both services succeed →
201 Created,domainStatus: "active". If either fails →207 Multi-Status,domainStatus: "pending".
2. Subdomain Provisioning
Triggered automatically after tenant creation, or manually via retry.
See Tenant Subdomain Provisioning for the full flow, retry sequence, architecture diagram, and environment variable reference.
3. Dynamic Schema Configuration
Triggered by a Super Admin via Configure Schema in the admin dashboard.
- Super Admin opens the Schema Builder for a tenant.
- Defines
jobSteps(name, order, isPaid) andarticleAttributes(name, label, type, isRequired). - Admin dashboard calls
POST /api/v1/tenant-configwith the full config payload. - API upserts the
TenantConfigdocument for that tenant. - The client app picks up the new schema on next login or context refresh.
See Dynamic Schema for full details.
4. Job Creation & Step Assignment
Triggered by a tenant admin in the client app.
sequenceDiagram
participant Admin as Tenant Admin
participant App as Client Dashboard
participant API as Core API
participant Ledger as Ledger DB
Admin->>App: Submits New Job (Article, Quantities)
App->>API: POST /job/create
API->>API: Instantiate Steps from TenantConfig
API->>API: Assign Workers to Steps (Status: Pending)
API-->>App: 200 OK (Job Created)
note over Admin,API: Later: Worker begins task
Admin->>App: Updates Stage to 'In-Progress'
App->>API: PATCH /job/:id (status: In-Progress)
API-->>App: 200 OK
note over Admin,Ledger: Later: Worker completes task
Admin->>App: Updates Stage to 'Completed'
App->>API: PATCH /job/:id (status: Completed)
API->>API: Calculate Wages (Article.stepRates * quantity)
API->>Ledger: Create Credit Entry for Worker
API-->>App: 200 OK (Stage Completed)
- Admin creates a job, selecting an article and filling in dynamic attribute fields (driven by
TenantConfig.articleAttributes). - Job steps are instantiated from
TenantConfig.jobSteps— each step starts withstatus: "Pending". - Workers are assigned to each step.
- As workers complete steps, status transitions:
Pending → In-Progress → Completed. - On step completion, the system calculates wages:
Article.stepRates[stepName] × totalQuantityand credits the worker'sLedger.
5. Wage & Ledger Calculation
Triggered automatically when a job step transitions to Completed.
jobControllerreadsArticle.stepRatesfor the completed step.- Multiplies the rate by the quantity produced in that step.
- Creates a
Ledgerentry (type: credit) for the assigned worker. - The worker's ledger is queryable via
POST /api/v1/ledger/worker/:workerId.
6. Tenant Isolation
An invisible workflow enforced on every API request.
authController.protectvalidates the JWT and attachesreq.user(includingtenantId).tenantMiddlewareinjects{ tenantId: req.user.tenantId }into all Mongoose queries via query hooks.- Operations like "Get Workers" or "Delete Job" automatically filter to the caller's tenant.
- Super Admins bypass tenant scoping for management operations (tenant routes use
restrictToSuperAdmininstead oftenantMiddleware).
7. Plan Limit Enforcement
Enforced at the API layer on write operations via the shared checkPlanLimit helper.
- Adding a job step beyond
Plan.limits.maxJobSteps→403 - Adding an article attribute beyond
Plan.limits.maxArticleAttributes→403 - Creating a user beyond
Plan.limits.maxUsers→403 - Creating a worker beyond
Plan.limits.maxWorkers→403 -1in any limit field means unlimited — no enforcement applied.
8. Worker Payment Settlement
Triggered when a payment transaction is created for a worker.
See Worker Payment Lifecycle for full details.
- Before recording the payment ledger entry, a settlement pass runs.
- Any existing advance credit (negative worker balance) is applied against
jobsPendingPaymententries in FIFO order. - The new payment amount is then applied against remaining
jobsPendingPaymententries in FIFO order. - Jobs whose full
earnedAmountis covered move fromjobsPendingPaymenttojobsPaid. - Any surplus beyond all pending jobs makes the worker's
balancego negative (advance credit for future jobs).
9. Multi-Client Onboarding
See Multi-Client Onboarding Guide for the full step-by-step process.
Summary:
- Create or select a Plan with appropriate limits and rate limits.
- Provision the tenant via the admin dashboard (atomic tenant + admin user creation).
- Configure the tenant: job steps, article attributes, size range, custom ledger types, pagination.
- Add CORS origin if the client uses a custom domain (takes effect within 60 seconds).
- Share credentials with the client's admin.