feat: add trip and trip settings with optimistic locking
This commit is contained in:
@@ -5,9 +5,16 @@ import { AppService } from './app.service';
|
||||
import { HealthModule } from './health/health.module';
|
||||
import { VersionModule } from './version/version.module';
|
||||
import { UsersApiModule } from './users/users.module';
|
||||
import { TripsApiModule } from './trips/trips.module';
|
||||
|
||||
@Module({
|
||||
imports: [ConfigurationModule, HealthModule, VersionModule, UsersApiModule],
|
||||
imports: [
|
||||
ConfigurationModule,
|
||||
HealthModule,
|
||||
VersionModule,
|
||||
UsersApiModule,
|
||||
TripsApiModule,
|
||||
],
|
||||
controllers: [AppController],
|
||||
providers: [AppService],
|
||||
})
|
||||
|
||||
73
backend/apps/api/src/trips/trips.controller.spec.ts
Normal file
73
backend/apps/api/src/trips/trips.controller.spec.ts
Normal file
@@ -0,0 +1,73 @@
|
||||
import { TripsController } from './trips.controller';
|
||||
import type { AuthenticatedUser } from '../../../../libs/auth/src';
|
||||
|
||||
describe('TripsController', () => {
|
||||
const currentUser: AuthenticatedUser = {
|
||||
id: 'u1',
|
||||
externalSubjectId: 'sub-1',
|
||||
displayName: 'Alex',
|
||||
email: 'a@example.com',
|
||||
};
|
||||
|
||||
it('GET /trips lists trips for the current user', async () => {
|
||||
const tripsService = {
|
||||
listTripsForUser: jest
|
||||
.fn()
|
||||
.mockResolvedValue([{ id: 't1', name: 'Slovenia 2027' }]),
|
||||
};
|
||||
const controller = new TripsController(tripsService as never);
|
||||
|
||||
await expect(controller.list(currentUser)).resolves.toEqual([
|
||||
{ id: 't1', name: 'Slovenia 2027' },
|
||||
]);
|
||||
expect(tripsService.listTripsForUser).toHaveBeenCalledWith('u1');
|
||||
});
|
||||
|
||||
it('POST /trips creates a trip owned by the current user', async () => {
|
||||
const created = {
|
||||
id: 't1',
|
||||
name: 'Slovenia 2027',
|
||||
ownerId: 'u1',
|
||||
version: 1,
|
||||
};
|
||||
const tripsService = { createTrip: jest.fn().mockResolvedValue(created) };
|
||||
const controller = new TripsController(tripsService as never);
|
||||
|
||||
await expect(
|
||||
controller.create(currentUser, { name: 'Slovenia 2027' }),
|
||||
).resolves.toEqual(created);
|
||||
expect(tripsService.createTrip).toHaveBeenCalledWith('u1', {
|
||||
name: 'Slovenia 2027',
|
||||
});
|
||||
});
|
||||
|
||||
it('GET /trips/:tripId returns a single trip', async () => {
|
||||
const trip = { id: 't1', name: 'Slovenia 2027' };
|
||||
const tripsService = { getTrip: jest.fn().mockResolvedValue(trip) };
|
||||
const controller = new TripsController(tripsService as never);
|
||||
|
||||
await expect(controller.getOne('t1')).resolves.toEqual(trip);
|
||||
});
|
||||
|
||||
it('PATCH /trips/:tripId forwards the update dto including version', async () => {
|
||||
const updated = { id: 't1', name: 'New name', version: 2 };
|
||||
const tripsService = { updateTrip: jest.fn().mockResolvedValue(updated) };
|
||||
const controller = new TripsController(tripsService as never);
|
||||
|
||||
await expect(
|
||||
controller.update('t1', { name: 'New name', version: 1 }),
|
||||
).resolves.toEqual(updated);
|
||||
expect(tripsService.updateTrip).toHaveBeenCalledWith('t1', {
|
||||
name: 'New name',
|
||||
version: 1,
|
||||
});
|
||||
});
|
||||
|
||||
it('DELETE /trips/:tripId deletes the trip', async () => {
|
||||
const tripsService = { deleteTrip: jest.fn().mockResolvedValue(undefined) };
|
||||
const controller = new TripsController(tripsService as never);
|
||||
|
||||
await controller.remove('t1');
|
||||
expect(tripsService.deleteTrip).toHaveBeenCalledWith('t1');
|
||||
});
|
||||
});
|
||||
56
backend/apps/api/src/trips/trips.controller.ts
Normal file
56
backend/apps/api/src/trips/trips.controller.ts
Normal file
@@ -0,0 +1,56 @@
|
||||
import {
|
||||
Body,
|
||||
Controller,
|
||||
Delete,
|
||||
Get,
|
||||
Param,
|
||||
Patch,
|
||||
Post,
|
||||
UseGuards,
|
||||
} from '@nestjs/common';
|
||||
import { OidcAuthGuard } from '../../../../libs/auth/src';
|
||||
import type { AuthenticatedUser } from '../../../../libs/auth/src';
|
||||
import { TripsService } from '../../../../libs/trips/src';
|
||||
import type {
|
||||
CreateTripDto,
|
||||
Trip,
|
||||
UpdateTripDto,
|
||||
} from '../../../../libs/trips/src';
|
||||
import { CurrentUser } from '../auth/current-user.decorator';
|
||||
|
||||
@Controller('trips')
|
||||
@UseGuards(OidcAuthGuard)
|
||||
export class TripsController {
|
||||
constructor(private readonly tripsService: TripsService) {}
|
||||
|
||||
@Get()
|
||||
list(@CurrentUser() currentUser: AuthenticatedUser): Promise<Trip[]> {
|
||||
return this.tripsService.listTripsForUser(currentUser.id);
|
||||
}
|
||||
|
||||
@Post()
|
||||
create(
|
||||
@CurrentUser() currentUser: AuthenticatedUser,
|
||||
@Body() dto: CreateTripDto,
|
||||
): Promise<Trip> {
|
||||
return this.tripsService.createTrip(currentUser.id, dto);
|
||||
}
|
||||
|
||||
@Get(':tripId')
|
||||
getOne(@Param('tripId') tripId: string): Promise<Trip> {
|
||||
return this.tripsService.getTrip(tripId);
|
||||
}
|
||||
|
||||
@Patch(':tripId')
|
||||
update(
|
||||
@Param('tripId') tripId: string,
|
||||
@Body() dto: UpdateTripDto,
|
||||
): Promise<Trip> {
|
||||
return this.tripsService.updateTrip(tripId, dto);
|
||||
}
|
||||
|
||||
@Delete(':tripId')
|
||||
remove(@Param('tripId') tripId: string): Promise<void> {
|
||||
return this.tripsService.deleteTrip(tripId);
|
||||
}
|
||||
}
|
||||
10
backend/apps/api/src/trips/trips.module.ts
Normal file
10
backend/apps/api/src/trips/trips.module.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { AuthModule } from '../../../../libs/auth/src';
|
||||
import { TripsLibModule } from '../../../../libs/trips/src';
|
||||
import { TripsController } from './trips.controller';
|
||||
|
||||
@Module({
|
||||
imports: [AuthModule, TripsLibModule],
|
||||
controllers: [TripsController],
|
||||
})
|
||||
export class TripsApiModule {}
|
||||
Reference in New Issue
Block a user