Files
trackgo-be/test/company-settings.e2e-spec.ts
T
shancheas 4b48dbdf3c Add timeline tracking features with database schema updates
- Introduced new tables `timeline_footprints` and `timeline_activities` to manage employee location data and activity records.
- Updated `company_settings` to include `gps_interval_seconds` and `checkout_warning_radius_meters` for enhanced tracking configuration.
- Implemented foreign key constraints to ensure data integrity between new tables and existing `employees`, `customers`, and `visits` tables.
- Created services and controllers for handling timeline activities and footprints, including ingestion and retrieval of data.
- Enhanced DTOs and validation logic to support new fields and ensure correct data formats in API requests.
- Added unit and integration tests to validate the new functionalities and ensure proper handling of timeline records.
- Created migration scripts to apply the necessary database schema changes for the new features.
2026-09-01 20:36:47 +07:00

130 lines
3.8 KiB
TypeScript

import { INestApplication } from '@nestjs/common';
import { Test, TestingModule } from '@nestjs/testing';
import { eq } from 'drizzle-orm';
import request from 'supertest';
import { App } from 'supertest/types';
import { AppModule } from '../src/app.module';
import { configureApp } from '../src/common/configure-app';
import { DRIZZLE, type DrizzleDB } from '../src/database/database.module';
import {
privilegeDetails,
privilegeKeys,
privileges,
users,
} from '../src/database/schema';
import { PRIVILEGE_ACTIONS } from '../src/modules/privileges/privilege-action';
import { registerAndActivate } from './helpers/activate-user';
describe('Company settings (e2e)', () => {
let app: INestApplication<App>;
let db: DrizzleDB;
const password = 'password123';
const adminUsername = `set_admin_${Date.now()}`;
const otherUsername = `set_other_${Date.now()}`;
let adminAccessToken: string;
let adminUserId: string;
let otherAccessToken: string;
beforeAll(async () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule],
}).compile();
app = moduleFixture.createNestApplication();
configureApp(app, {
NODE_ENV: 'test',
SWAGGER_ENABLED: 'false',
});
await app.init();
db = app.get(DRIZZLE);
const admin = await registerAndActivate(app, db, adminUsername, password);
adminAccessToken = admin.accessToken;
adminUserId = admin.userId;
const other = await registerAndActivate(app, db, otherUsername, password);
otherAccessToken = other.accessToken;
const now = Date.now();
const [priv] = await db
.insert(privileges)
.values({
name: 'Settings Admin',
code: `SET_ADMIN_${now}`,
status: 'active',
createdAt: now,
updatedAt: now,
createdBy: adminUserId,
updatedBy: adminUserId,
})
.returning();
const keys = await db.select().from(privilegeKeys);
const detailRows = keys.flatMap((key) =>
PRIVILEGE_ACTIONS.map((action) => ({
privilegeId: priv.id,
privilegeKeyId: key.id,
action,
value: true,
})),
);
await db.insert(privilegeDetails).values(detailRows);
await db
.update(users)
.set({ privilegeId: priv.id, updatedAt: Date.now() })
.where(eq(users.id, adminUserId));
});
afterAll(async () => {
await app.close();
});
it('forbids settings without permission', async () => {
await request(app.getHttpServer())
.get('/settings')
.set('Authorization', `Bearer ${otherAccessToken}`)
.expect(403);
});
it('upserts the cycle start date', async () => {
const updated = await request(app.getHttpServer())
.patch('/settings')
.set('Authorization', `Bearer ${adminAccessToken}`)
.send({ cycleStartDate: '2026-01-05' })
.expect(200);
expect((updated.body as { cycleStartDate: number }).cycleStartDate).toBe(
Date.parse('2026-01-04T17:00:00.000Z'),
);
const got = await request(app.getHttpServer())
.get('/settings')
.set('Authorization', `Bearer ${adminAccessToken}`)
.expect(200);
expect((got.body as { cycleStartDate: number }).cycleStartDate).toBe(
(updated.body as { cycleStartDate: number }).cycleStartDate,
);
});
it('upserts timeline tracking settings', async () => {
const updated = await request(app.getHttpServer())
.patch('/settings')
.set('Authorization', `Bearer ${adminAccessToken}`)
.send({
gpsIntervalSeconds: 15,
checkoutWarningRadiusMeters: 300,
})
.expect(200);
expect((updated.body as { gpsIntervalSeconds: number }).gpsIntervalSeconds).toBe(
15,
);
expect(
(updated.body as { checkoutWarningRadiusMeters: number })
.checkoutWarningRadiusMeters,
).toBe(300);
});
});