fix: block assigning a colony to a queen who is no longer available
assignColony() never checked the queen's own status, so a queen marked LOST/DECEASED/REPLACED/SOLD - exactly the statuses the queen-loss fix now treats as "not really available" and releases her from her old colony for - could still be assigned to a brand new colony, which makes no domain sense (you can't put a dead queen in a hive). Rejects with ConflictException up front, before even looking up the destination colony; correcting a mistaken status update first is the intended fix path. Co-Authored-By: Claude Sonnet 5 <[email protected]>
This commit is contained in:
1 parent
5c5c7b756f
commit
3e179391b0
2 files changed
+22
-1
No files matched your search
@@ -1,4 +1,8 @@
|
|||||||
import { BadRequestException, NotFoundException } from '@nestjs/common';
|
import {
|
||||||
|
BadRequestException,
|
||||||
|
ConflictException,
|
||||||
|
NotFoundException,
|
||||||
|
} from '@nestjs/common';
|
||||||
import { QueensService } from './queens.service';
|
import { QueensService } from './queens.service';
|
||||||
import { PrismaService } from '../prisma/prisma.service';
|
import { PrismaService } from '../prisma/prisma.service';
|
||||||
import { AuditLogService } from '../audit/audit-log.service';
|
import { AuditLogService } from '../audit/audit-log.service';
|
||||||
@@ -78,6 +82,17 @@ describe('QueensService', () => {
|
|||||||
).rejects.toBeInstanceOf(NotFoundException);
|
).rejects.toBeInstanceOf(NotFoundException);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('assignColony rejects a queen who is no longer available', async () => {
|
||||||
|
prisma.queen.findUnique.mockResolvedValue({
|
||||||
|
...queen,
|
||||||
|
status: 'DECEASED',
|
||||||
|
});
|
||||||
|
await expect(
|
||||||
|
service.assignColony('queen-1', { colonyId: 'colony-1' }, 'user-1'),
|
||||||
|
).rejects.toBeInstanceOf(ConflictException);
|
||||||
|
expect(prisma.colony.findUnique).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
it('assignColony closes the previous queen history, sets currentQueenId, and opens a new history row', async () => {
|
it('assignColony closes the previous queen history, sets currentQueenId, and opens a new history row', async () => {
|
||||||
prisma.queen.findUnique.mockResolvedValue(queen);
|
prisma.queen.findUnique.mockResolvedValue(queen);
|
||||||
prisma.colony.findUnique.mockResolvedValue({
|
prisma.colony.findUnique.mockResolvedValue({
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import {
|
import {
|
||||||
BadRequestException,
|
BadRequestException,
|
||||||
|
ConflictException,
|
||||||
Injectable,
|
Injectable,
|
||||||
NotFoundException,
|
NotFoundException,
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
@@ -154,6 +155,11 @@ export class QueensService {
|
|||||||
actingUserId: string,
|
actingUserId: string,
|
||||||
): Promise<Queen> {
|
): Promise<Queen> {
|
||||||
const queen = await this.findById(id);
|
const queen = await this.findById(id);
|
||||||
|
if (QUEEN_NO_LONGER_AVAILABLE_STATUSES.includes(queen.status)) {
|
||||||
|
throw new ConflictException(
|
||||||
|
`Queen ${id} is not available for assignment (status: ${queen.status}) - correct her status first if this was a mistake`,
|
||||||
|
);
|
||||||
|
}
|
||||||
const colony = await this.prisma.colony.findUnique({
|
const colony = await this.prisma.colony.findUnique({
|
||||||
where: { id: dto.colonyId },
|
where: { id: dto.colonyId },
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in new issue
Block a user