mirror of
https://github.com/immich-app/immich
synced 2025-11-14 17:36:12 +00:00
feat(web): Remove from Stack (#19703)
* - add component - update server's StackCreateDto for merge parameter - Update stackRepo to only merge stacks when merge=true (default) - update web action handlers to show stack changes * - make open-api * lint & format * - Add proper icon to 'remove from stack' - change web unstack icon to image-off-outline * - cleanup * - format & lint * - make open-api: StackCreateDto merge optional * initial addition of new endpoint * remove stack endpoint * - fix up remove stack endpoint - open-api * - Undo stackCreate merge parameter * - open-api typescript * open-api dart * Tests: - add tests - update assetStub.imageFrom2015 to have required stack attributes to include it with tests * update event name * Fix event name in test * remove asset_update check * - merge stack.removeAsset params into one object - refactor asset existence check (no need for asset fetch) - fix tests * Don't return updated stack * Create specialized stack id & primary asset fetch for asset removal checks * Correct new permission names * make sql * - fix open-api * - cleanup
This commit is contained in:
parent
1011cdb376
commit
1a70896113
19 changed files with 289 additions and 23 deletions
|
|
@ -6,7 +6,7 @@ import { StackCreateDto, StackResponseDto, StackSearchDto, StackUpdateDto } from
|
|||
import { Permission } from 'src/enum';
|
||||
import { Auth, Authenticated } from 'src/middleware/auth.guard';
|
||||
import { StackService } from 'src/services/stack.service';
|
||||
import { UUIDParamDto } from 'src/validation';
|
||||
import { UUIDAssetIDParamDto, UUIDParamDto } from 'src/validation';
|
||||
|
||||
@ApiTags('Stacks')
|
||||
@Controller('stacks')
|
||||
|
|
@ -54,4 +54,11 @@ export class StackController {
|
|||
deleteStack(@Auth() auth: AuthDto, @Param() { id }: UUIDParamDto): Promise<void> {
|
||||
return this.service.delete(auth, id);
|
||||
}
|
||||
|
||||
@Delete(':id/assets/:assetId')
|
||||
@Authenticated({ permission: Permission.StackUpdate })
|
||||
@HttpCode(HttpStatus.NO_CONTENT)
|
||||
removeAssetFromStack(@Auth() auth: AuthDto, @Param() dto: UUIDAssetIDParamDto): Promise<void> {
|
||||
return this.service.removeAsset(auth, dto);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -143,3 +143,13 @@ from
|
|||
"stack"
|
||||
where
|
||||
"id" = $1::uuid
|
||||
|
||||
-- StackRepository.getForAssetRemoval
|
||||
select
|
||||
"stackId" as "id",
|
||||
"stack"."primaryAssetId"
|
||||
from
|
||||
"asset"
|
||||
left join "stack" on "stack"."id" = "asset"."stackId"
|
||||
where
|
||||
"asset"."id" = $1
|
||||
|
|
|
|||
|
|
@ -152,4 +152,14 @@ export class StackRepository {
|
|||
.where('id', '=', asUuid(id))
|
||||
.executeTakeFirst();
|
||||
}
|
||||
|
||||
@GenerateSql({ params: [DummyValue.UUID, DummyValue.UUID] })
|
||||
getForAssetRemoval(assetId: string) {
|
||||
return this.db
|
||||
.selectFrom('asset')
|
||||
.leftJoin('stack', 'stack.id', 'asset.stackId')
|
||||
.select(['stackId as id', 'stack.primaryAssetId'])
|
||||
.where('asset.id', '=', assetId)
|
||||
.executeTakeFirst();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -188,4 +188,53 @@ describe(StackService.name, () => {
|
|||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('removeAsset', () => {
|
||||
it('should require stack.update permissions', async () => {
|
||||
await expect(sut.removeAsset(authStub.admin, { id: 'stack-id', assetId: 'asset-id' })).rejects.toBeInstanceOf(
|
||||
BadRequestException,
|
||||
);
|
||||
|
||||
expect(mocks.stack.getForAssetRemoval).not.toHaveBeenCalled();
|
||||
expect(mocks.asset.update).not.toHaveBeenCalled();
|
||||
expect(mocks.event.emit).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should fail if the asset is not in the stack', async () => {
|
||||
mocks.access.stack.checkOwnerAccess.mockResolvedValue(new Set(['stack-id']));
|
||||
mocks.stack.getForAssetRemoval.mockResolvedValue({ id: null, primaryAssetId: null });
|
||||
|
||||
await expect(
|
||||
sut.removeAsset(authStub.admin, { id: 'stack-id', assetId: assetStub.imageFrom2015.id }),
|
||||
).rejects.toBeInstanceOf(BadRequestException);
|
||||
|
||||
expect(mocks.asset.update).not.toHaveBeenCalled();
|
||||
expect(mocks.event.emit).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should fail if the assetId is the primaryAssetId', async () => {
|
||||
mocks.access.stack.checkOwnerAccess.mockResolvedValue(new Set(['stack-id']));
|
||||
mocks.stack.getForAssetRemoval.mockResolvedValue({ id: 'stack-id', primaryAssetId: assetStub.image.id });
|
||||
|
||||
await expect(
|
||||
sut.removeAsset(authStub.admin, { id: 'stack-id', assetId: assetStub.image.id }),
|
||||
).rejects.toBeInstanceOf(BadRequestException);
|
||||
|
||||
expect(mocks.asset.update).not.toHaveBeenCalled();
|
||||
expect(mocks.event.emit).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("should update the asset to nullify it's stack-id", async () => {
|
||||
mocks.access.stack.checkOwnerAccess.mockResolvedValue(new Set(['stack-id']));
|
||||
mocks.stack.getForAssetRemoval.mockResolvedValue({ id: 'stack-id', primaryAssetId: assetStub.image.id });
|
||||
|
||||
await sut.removeAsset(authStub.admin, { id: 'stack-id', assetId: assetStub.image1.id });
|
||||
|
||||
expect(mocks.asset.update).toHaveBeenCalledWith({ id: assetStub.image1.id, stackId: null });
|
||||
expect(mocks.event.emit).toHaveBeenCalledWith('StackUpdate', {
|
||||
stackId: 'stack-id',
|
||||
userId: authStub.admin.user.id,
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ import { AuthDto } from 'src/dtos/auth.dto';
|
|||
import { StackCreateDto, StackResponseDto, StackSearchDto, StackUpdateDto, mapStack } from 'src/dtos/stack.dto';
|
||||
import { Permission } from 'src/enum';
|
||||
import { BaseService } from 'src/services/base.service';
|
||||
import { UUIDAssetIDParamDto } from 'src/validation';
|
||||
|
||||
@Injectable()
|
||||
export class StackService extends BaseService {
|
||||
|
|
@ -58,6 +59,24 @@ export class StackService extends BaseService {
|
|||
await this.eventRepository.emit('StackDeleteAll', { stackIds: dto.ids, userId: auth.user.id });
|
||||
}
|
||||
|
||||
async removeAsset(auth: AuthDto, dto: UUIDAssetIDParamDto): Promise<void> {
|
||||
const { id: stackId, assetId } = dto;
|
||||
await this.requireAccess({ auth, permission: Permission.StackUpdate, ids: [stackId] });
|
||||
|
||||
const stack = await this.stackRepository.getForAssetRemoval(assetId);
|
||||
|
||||
if (!stack?.id || stack.id !== stackId) {
|
||||
throw new BadRequestException('Asset not in stack');
|
||||
}
|
||||
|
||||
if (stack.primaryAssetId === assetId) {
|
||||
throw new BadRequestException("Cannot remove stack's primary asset");
|
||||
}
|
||||
|
||||
await this.assetRepository.update({ id: assetId, stackId: null });
|
||||
await this.eventRepository.emit('StackUpdate', { stackId, userId: auth.user.id });
|
||||
}
|
||||
|
||||
private async findOrFail(id: string) {
|
||||
const stack = await this.stackRepository.getById(id);
|
||||
if (!stack) {
|
||||
|
|
|
|||
|
|
@ -63,6 +63,22 @@ export class FileNotEmptyValidator extends FileValidator {
|
|||
}
|
||||
}
|
||||
|
||||
type UUIDOptions = { optional?: boolean; each?: boolean; nullable?: boolean };
|
||||
export const ValidateUUID = (options?: UUIDOptions & ApiPropertyOptions) => {
|
||||
const { optional, each, nullable, ...apiPropertyOptions } = {
|
||||
optional: false,
|
||||
each: false,
|
||||
nullable: false,
|
||||
...options,
|
||||
};
|
||||
return applyDecorators(
|
||||
IsUUID('4', { each }),
|
||||
ApiProperty({ format: 'uuid', ...apiPropertyOptions }),
|
||||
optional ? Optional({ nullable }) : IsNotEmpty(),
|
||||
each ? IsArray() : IsString(),
|
||||
);
|
||||
};
|
||||
|
||||
export class UUIDParamDto {
|
||||
@IsNotEmpty()
|
||||
@IsUUID('4')
|
||||
|
|
@ -70,6 +86,14 @@ export class UUIDParamDto {
|
|||
id!: string;
|
||||
}
|
||||
|
||||
export class UUIDAssetIDParamDto {
|
||||
@ValidateUUID()
|
||||
id!: string;
|
||||
|
||||
@ValidateUUID()
|
||||
assetId!: string;
|
||||
}
|
||||
|
||||
type PinCodeOptions = { optional?: boolean } & OptionalOptions;
|
||||
export const PinCode = (options?: PinCodeOptions & ApiPropertyOptions) => {
|
||||
const { optional, nullable, emptyToNull, ...apiPropertyOptions } = {
|
||||
|
|
@ -131,22 +155,6 @@ export const ValidateHexColor = () => {
|
|||
return applyDecorators(...decorators);
|
||||
};
|
||||
|
||||
type UUIDOptions = { optional?: boolean; each?: boolean; nullable?: boolean };
|
||||
export const ValidateUUID = (options?: UUIDOptions & ApiPropertyOptions) => {
|
||||
const { optional, each, nullable, ...apiPropertyOptions } = {
|
||||
optional: false,
|
||||
each: false,
|
||||
nullable: false,
|
||||
...options,
|
||||
};
|
||||
return applyDecorators(
|
||||
IsUUID('4', { each }),
|
||||
ApiProperty({ format: 'uuid', ...apiPropertyOptions }),
|
||||
optional ? Optional({ nullable }) : IsNotEmpty(),
|
||||
each ? IsArray() : IsString(),
|
||||
);
|
||||
};
|
||||
|
||||
type DateOptions = { optional?: boolean; nullable?: boolean; format?: 'date' | 'date-time' };
|
||||
export const ValidateDate = (options?: DateOptions & ApiPropertyOptions) => {
|
||||
const { optional, nullable, format, ...apiPropertyOptions } = {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue