Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
452 changes: 452 additions & 0 deletions apps/api-e2e/src/api/request-revision.e2e.spec.ts

Large diffs are not rendered by default.

1,224 changes: 1,224 additions & 0 deletions apps/api-e2e/src/api/targeted-time-corrections.e2e.spec.ts

Large diffs are not rendered by default.

572 changes: 572 additions & 0 deletions apps/api-e2e/src/api/time-entry-approvals.e2e.spec.ts

Large diffs are not rendered by default.

831 changes: 772 additions & 59 deletions apps/api/openapi.json

Large diffs are not rendered by default.

13 changes: 10 additions & 3 deletions apps/api/src/app/accounts/vacation-balance.service.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { Prisma } from '@prisma/client';
import { Injectable } from '@nestjs/common';
import { calculateWorkingDays } from 'shared';
import { LeaveAllowancesService } from '../leave-allowances/leave-allowances.service';
Expand All @@ -20,16 +21,22 @@ export class VacationBalanceService {
private readonly schedules: WorkSchedulesService,
) {}

async compute(employeeId: string, year: number): Promise<VacationBalanceDto> {
const allowance = await this.allowances.getOrDefault(employeeId, year);
async compute(
employeeId: string,
year: number,
excludeRequestId?: string,
db: Prisma.TransactionClient = this.prisma,
): Promise<VacationBalanceDto> {
const allowance = await this.allowances.getOrDefault(employeeId, year, db);
const schedule = await this.schedules.resolveForEmployee(employeeId);
const yearStart = new Date(Date.UTC(year, 0, 1));
const yearEnd = new Date(Date.UTC(year, 11, 31, 23, 59, 59));

const requests = await this.prisma.request.findMany({
const requests = await db.request.findMany({
where: {
employeeId,
type: 'Vacation',
...(excludeRequestId ? { id: { not: excludeRequestId } } : {}),
from: { lte: yearEnd },
to: { gte: yearStart },
},
Expand Down
16 changes: 10 additions & 6 deletions apps/api/src/app/leave-allowances/leave-allowances.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Injectable, NotFoundException } from '@nestjs/common';
import type { EmployeeLeaveAllowance } from '@prisma/client';
import type { EmployeeLeaveAllowance, Prisma } from '@prisma/client';
import type { JwtUser } from '../auth/jwt.strategy';
import { EmployeesService } from '../employees/employees.service';
import { PrismaService } from '../prisma/prisma.service';
Expand Down Expand Up @@ -29,13 +29,14 @@ export class LeaveAllowancesService {
async getOrDefault(
employeeId: string,
year: number,
db: Prisma.TransactionClient = this.prisma,
): Promise<EmployeeLeaveAllowance> {
const existing = await this.prisma.employeeLeaveAllowance.findUnique({
const existing = await db.employeeLeaveAllowance.findUnique({
where: { employeeId_year: { employeeId, year } },
});
if (existing) return existing;
const employee = await this.assertEmployee(employeeId);
return this.prisma.employeeLeaveAllowance.create({
const employee = await this.assertEmployee(employeeId, db);
return db.employeeLeaveAllowance.create({
data: {
employeeId,
year,
Expand Down Expand Up @@ -113,8 +114,11 @@ export class LeaveAllowancesService {
return { scanned: candidates.length, expired };
}

private async assertEmployee(employeeId: string) {
const employee = await this.prisma.employee.findUnique({
private async assertEmployee(
employeeId: string,
db: Prisma.TransactionClient = this.prisma,
) {
const employee = await db.employee.findUnique({
where: { id: employeeId },
});
if (!employee)
Expand Down
72 changes: 70 additions & 2 deletions apps/api/src/app/requests/requests.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,25 @@ import {
Controller,
Get,
Param,
Patch,
ParseUUIDPipe,
Post,
Query,
UseGuards,
} from '@nestjs/common';
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
import {
ApiBearerAuth,
ApiCreatedResponse,
ApiOkResponse,
ApiTags,
} from '@nestjs/swagger';
import { CurrentUser } from '../auth/current-user.decorator';
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
import type { JwtUser } from '../auth/jwt.strategy';
import {
TimeEntryDto,
TimeEntryAuditDto,
} from '../time-entries/time-entries.dto';
import { RequestsService } from './requests.service';
import {
BulkApproveDto,
Expand All @@ -20,9 +30,10 @@ import {
CreateVacationDto,
ManagerApproveDto,
TransitionDto,
UpdateDraftRequestDto,
TransitionWithRequiredNoteDto,
type BulkResult,
type RequestDto,
RequestDto,
type RequestEventDto,
} from './requests.dto';

Expand All @@ -34,6 +45,7 @@ export class RequestsController {
constructor(private readonly service: RequestsService) {}

@Get()
@ApiOkResponse({ type: RequestDto, isArray: true })
list(
@CurrentUser() user: JwtUser,
@Query('employeeId') employeeId?: string,
Expand All @@ -56,7 +68,31 @@ export class RequestsController {
);
}

@Get('time-adjustment-targets')
@ApiOkResponse({ type: TimeEntryDto, isArray: true })
timeAdjustmentTargets(
@CurrentUser() user: JwtUser,
@Query('from') from?: string,
@Query('to') to?: string,
): Promise<TimeEntryDto[]> {
return this.service.timeAdjustmentTargets(
user.id,
from ? new Date(from) : undefined,
to ? new Date(to) : undefined,
);
}

@Get(':id/time-entry-audit')
@ApiOkResponse({ type: TimeEntryAuditDto, isArray: true })
timeEntryAudit(
@Param('id', new ParseUUIDPipe()) id: string,
@CurrentUser() user: JwtUser,
): Promise<TimeEntryAuditDto[]> {
return this.service.timeEntryAudit(id, user.id);
}

@Get(':id')
@ApiOkResponse({ type: RequestDto })
get(
@Param('id', new ParseUUIDPipe()) id: string,
@CurrentUser() user: JwtUser,
Expand All @@ -73,6 +109,7 @@ export class RequestsController {
}

@Post()
@ApiCreatedResponse({ type: RequestDto })
create(
@Body() dto: CreateRequestDto,
@CurrentUser() user: JwtUser,
Expand All @@ -81,16 +118,38 @@ export class RequestsController {
}

@Post('vacation')
@ApiCreatedResponse({ type: RequestDto })
createVacation(
@Body() dto: CreateVacationDto,
@CurrentUser() user: JwtUser,
): Promise<RequestDto> {
return this.service.createVacation({ ...dto, employeeId: user.id });
}

@Patch(':id')
@ApiOkResponse({ type: RequestDto })
updateDraft(
@Param('id', new ParseUUIDPipe()) id: string,
@Body() dto: UpdateDraftRequestDto,
@CurrentUser() user: JwtUser,
): Promise<RequestDto> {
return this.service.updateDraft(id, user.id, dto, false);
}

@Post(':id/resubmit')
@ApiCreatedResponse({ type: RequestDto })
resubmit(
@Param('id', new ParseUUIDPipe()) id: string,
@Body() dto: UpdateDraftRequestDto,
@CurrentUser() user: JwtUser,
): Promise<RequestDto> {
return this.service.updateDraft(id, user.id, dto, true);
}

// ----- Generic approve / reject (legacy convenience) -----

@Post(':id/approve')
@ApiCreatedResponse({ type: RequestDto })
approve(
@Param('id', new ParseUUIDPipe()) id: string,
@Body() body: TransitionDto,
Expand All @@ -100,6 +159,7 @@ export class RequestsController {
}

@Post(':id/reject')
@ApiCreatedResponse({ type: RequestDto })
reject(
@Param('id', new ParseUUIDPipe()) id: string,
@Body() body: TransitionDto,
Expand All @@ -111,6 +171,7 @@ export class RequestsController {
// ----- Vacation workflow -----

@Post(':id/manager-approve')
@ApiCreatedResponse({ type: RequestDto })
managerApprove(
@Param('id', new ParseUUIDPipe()) id: string,
@Body() body: ManagerApproveDto,
Expand All @@ -125,6 +186,7 @@ export class RequestsController {
}

@Post(':id/manager-reject')
@ApiCreatedResponse({ type: RequestDto })
managerReject(
@Param('id', new ParseUUIDPipe()) id: string,
@Body() body: TransitionDto,
Expand All @@ -134,6 +196,7 @@ export class RequestsController {
}

@Post(':id/hr-confirm')
@ApiCreatedResponse({ type: RequestDto })
hrConfirm(
@Param('id', new ParseUUIDPipe()) id: string,
@Body() body: TransitionDto,
Expand All @@ -143,6 +206,7 @@ export class RequestsController {
}

@Post(':id/hr-reject')
@ApiCreatedResponse({ type: RequestDto })
hrReject(
@Param('id', new ParseUUIDPipe()) id: string,
@Body() body: TransitionWithRequiredNoteDto,
Expand All @@ -152,6 +216,7 @@ export class RequestsController {
}

@Post(':id/substitute/accept')
@ApiCreatedResponse({ type: RequestDto })
substituteAccept(
@Param('id', new ParseUUIDPipe()) id: string,
@Body() body: TransitionDto,
Expand All @@ -161,6 +226,7 @@ export class RequestsController {
}

@Post(':id/substitute/decline')
@ApiCreatedResponse({ type: RequestDto })
substituteDecline(
@Param('id', new ParseUUIDPipe()) id: string,
@Body() body: TransitionWithRequiredNoteDto,
Expand All @@ -170,6 +236,7 @@ export class RequestsController {
}

@Post(':id/return')
@ApiCreatedResponse({ type: RequestDto })
returnForRevision(
@Param('id', new ParseUUIDPipe()) id: string,
@Body() body: TransitionWithRequiredNoteDto,
Expand All @@ -179,6 +246,7 @@ export class RequestsController {
}

@Post(':id/cancel')
@ApiCreatedResponse({ type: RequestDto })
cancel(
@Param('id', new ParseUUIDPipe()) id: string,
@Body() body: TransitionDto,
Expand Down
Loading
Loading