Skip to content
Merged
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
51 changes: 51 additions & 0 deletions src/admin/controllers/__tests__/admin-puzzles.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ describe('AdminPuzzlesController', () => {
create: jest.fn(),
update: jest.fn(),
remove: jest.fn(),
archive: jest.fn(),
restore: jest.fn(),
},
},
{
Expand Down Expand Up @@ -71,4 +73,53 @@ describe('AdminPuzzlesController', () => {
});
});
});

describe('archive', () => {
it('should call puzzlesService.archive and auditLogService.log', async () => {
const mockPuzzle = { id: 'puzzle-id', title: 'Archived Puzzle' };
(puzzlesService.archive as jest.Mock).mockResolvedValue(mockPuzzle);

const result = await controller.archive('puzzle-id', mockUser);

expect(puzzlesService.archive).toHaveBeenCalledWith(
'puzzle-id',
mockUser.id,
);
expect(auditLogService.log).toHaveBeenCalledWith({
adminId: mockUser.id,
action: 'ARCHIVE_PUZZLE',
targetType: 'PUZZLE',
targetId: 'puzzle-id',
details: { title: mockPuzzle.title },
});
expect(result).toEqual(mockPuzzle);
});
});

describe('restore', () => {
it('should call puzzlesService.restore and auditLogService.log with actor details', async () => {
const mockPuzzle = { id: 'puzzle-id', title: 'Restored Puzzle' };
(puzzlesService.restore as jest.Mock).mockResolvedValue(mockPuzzle);

const result = await controller.restore('puzzle-id', mockUser);

expect(puzzlesService.restore).toHaveBeenCalledWith(
'puzzle-id',
mockUser.id,
);
expect(auditLogService.log).toHaveBeenCalledWith(
expect.objectContaining({
adminId: mockUser.id,
action: 'RESTORE_PUZZLE',
targetType: 'PUZZLE',
targetId: 'puzzle-id',
details: expect.objectContaining({
title: mockPuzzle.title,
restoredBy: mockUser.id,
}),
}),
);
expect(result).toEqual(mockPuzzle);
});
});
});
36 changes: 36 additions & 0 deletions src/admin/controllers/admin-puzzles.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,40 @@ export class AdminPuzzlesController {
targetId: id,
});
}

@Post(':id/archive')
async archive(
@Param('id', ParseUUIDPipe) id: string,
@ActiveUser() user: any,
) {
const puzzle = await this.puzzlesService.archive(id, user.id);
await this.auditLogService.log({
adminId: user.id,
action: 'ARCHIVE_PUZZLE',
targetType: 'PUZZLE',
targetId: id,
details: { title: puzzle.title },
});
return puzzle;
}

@Post(':id/restore')
async restore(
@Param('id', ParseUUIDPipe) id: string,
@ActiveUser() user: any,
) {
const puzzle = await this.puzzlesService.restore(id, user.id);
await this.auditLogService.log({
adminId: user.id,
action: 'RESTORE_PUZZLE',
targetType: 'PUZZLE',
targetId: id,
details: {
title: puzzle.title,
restoredAt: new Date(),
restoredBy: user.id,
},
});
return puzzle;
}
}
1 change: 1 addition & 0 deletions src/puzzles/dto/bulk-operations.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export enum BulkAction {
PUBLISH = 'publish',
UNPUBLISH = 'unpublish',
ARCHIVE = 'archive',
RESTORE = 'restore',
DELETE = 'delete',
UPDATE_CATEGORY = 'update_category',
ADD_TAGS = 'add_tags',
Expand Down
5 changes: 5 additions & 0 deletions src/puzzles/dto/search-puzzle.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ export class SearchPuzzleDto {
@Transform(({ value }: any) => value === 'true')
isPublished?: boolean;

@IsOptional()
@IsBoolean()
@Transform(({ value }: any) => value === 'true' || value === true)
isArchived?: boolean;

@IsOptional()
@IsString()
createdBy?: string;
Expand Down
10 changes: 10 additions & 0 deletions src/puzzles/entities/puzzle.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ import { Tag } from './tag.entity';
export class Puzzle {
@Column({ type: 'timestamp with time zone', nullable: true })
archivedAt?: Date;

@Column({ type: 'timestamp with time zone', nullable: true })
restoredAt?: Date;

@Column({ type: 'uuid', nullable: true })
restoredBy?: string;
@PrimaryGeneratedColumn('uuid')
id: string;

Expand Down Expand Up @@ -188,6 +194,10 @@ export class Puzzle {
hard: number;
expert: number;
};
archivedAt?: string;
archivedBy?: string;
restoredAt?: string;
restoredBy?: string;
};

@CreateDateColumn()
Expand Down
20 changes: 20 additions & 0 deletions src/puzzles/puzzles.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,26 @@ export class PuzzlesController {
return await this.puzzlesService.update(id, { isPublished: false }, userId);
}

@Post(':id/archive')
async archive(
@Param('id', ParseUUIDPipe) id: string,
@Req() req: any,
): Promise<PuzzleWithStats> {
const userId = req.user?.id ?? 'temp-user-id';
this.logger.log(`Archiving puzzle: ${id} by user: ${userId}`);
return await this.puzzlesService.archive(id, userId);
}

@Post(':id/restore')
async restore(
@Param('id', ParseUUIDPipe) id: string,
@Req() req: any,
): Promise<PuzzleWithStats> {
const userId = req.user?.id ?? 'temp-user-id';
this.logger.log(`Restoring puzzle: ${id} by user: ${userId}`);
return await this.puzzlesService.restore(id, userId);
}

@Post(':id/duplicate')
async duplicate(
@Param('id', ParseUUIDPipe) id: string,
Expand Down
Loading