Add user and employee management enhancements with database schema updates
- Introduced new columns `status`, `created_by`, and `updated_by` in the `users` table to track user status and ownership. - Updated the `employees` table to include a foreign key reference to the `users` table via `user_id`. - Created migration script `0012_users_primary.sql` to apply these changes to the database schema. - Enhanced the `EmployeesService` and `EmployeesRepository` to support user assignments and related data retrieval. - Updated DTOs and service methods to reflect the new user and employee relationships. - Added unit tests to validate the new functionality and ensure data integrity. - Modified existing controllers to accommodate the new fields and relationships in user and employee management.
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
import { INestApplication } from '@nestjs/common';
|
||||
import { eq } from 'drizzle-orm';
|
||||
import request from 'supertest';
|
||||
import { users } from '../../src/database/schema';
|
||||
import type { DrizzleDB } from '../../src/database/database.module';
|
||||
|
||||
export async function registerAndActivate(
|
||||
app: INestApplication,
|
||||
db: DrizzleDB,
|
||||
username: string,
|
||||
password: string,
|
||||
): Promise<{ accessToken: string; refreshToken: string; userId: string }> {
|
||||
const register = await request(app.getHttpServer())
|
||||
.post('/auth/register')
|
||||
.send({ username, password });
|
||||
if (register.status !== 201) {
|
||||
throw new Error(
|
||||
`register failed ${register.status} ${JSON.stringify(register.body)}`,
|
||||
);
|
||||
}
|
||||
const userId = (register.body as { id: string }).id;
|
||||
await db
|
||||
.update(users)
|
||||
.set({ status: 'active' })
|
||||
.where(eq(users.id, userId));
|
||||
const login = await request(app.getHttpServer())
|
||||
.post('/auth/login')
|
||||
.send({ username, password });
|
||||
if (login.status !== 200) {
|
||||
throw new Error(
|
||||
`login failed ${login.status} ${JSON.stringify(login.body)}`,
|
||||
);
|
||||
}
|
||||
const tokens = login.body as { accessToken: string; refreshToken: string };
|
||||
return {
|
||||
userId,
|
||||
accessToken: tokens.accessToken,
|
||||
refreshToken: tokens.refreshToken,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user