Skip to content

Commit 83b8e84

Browse files
author
Artem
committed
#RI-2939 BE Delete Consumer Group
1 parent f16336b commit 83b8e84

File tree

4 files changed

+112
-3
lines changed

4 files changed

+112
-3
lines changed

redisinsight/api/src/modules/browser/constants/browser-tool-commands.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ export enum BrowserToolStreamCommands {
8484
XPending = 'xpending',
8585
XGroupCreate = 'xgroup create',
8686
XGroupSetId = 'xgroup setid',
87+
XGroupDestroy = 'xgroup destroy',
8788
}
8889

8990
export enum BrowserToolTSCommands {

redisinsight/api/src/modules/browser/controllers/stream/consumer-group.controller.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {
22
Body,
3-
Controller,
3+
Controller, Delete,
44
Param, Patch,
55
Post,
66
UsePipes,
@@ -9,7 +9,11 @@ import {
99
import { ApiTags } from '@nestjs/swagger';
1010
import { ApiRedisInstanceOperation } from 'src/decorators/api-redis-instance-operation.decorator';
1111
import {
12-
ConsumerGroupDto, CreateConsumerGroupsDto, UpdateConsumerGroupDto,
12+
ConsumerGroupDto,
13+
CreateConsumerGroupsDto,
14+
DeleteConsumerGroupsDto,
15+
DeleteConsumerGroupsResponse,
16+
UpdateConsumerGroupDto,
1317
} from 'src/modules/browser/dto/stream.dto';
1418
import { ConsumerGroupService } from 'src/modules/browser/services/stream/consumer-group.service';
1519
import { KeyDto } from 'src/modules/browser/dto';
@@ -63,4 +67,23 @@ export class ConsumerGroupController {
6367
): Promise<void> {
6468
return this.service.updateGroup({ instanceId }, dto);
6569
}
70+
71+
@Delete('')
72+
@ApiRedisInstanceOperation({
73+
description: 'Delete Consumer Group',
74+
statusCode: 200,
75+
responses: [
76+
{
77+
status: 200,
78+
description: 'Returns number of affected consumer groups.',
79+
type: DeleteConsumerGroupsResponse,
80+
},
81+
],
82+
})
83+
async deleteGroup(
84+
@Param('dbInstance') instanceId: string,
85+
@Body() dto: DeleteConsumerGroupsDto,
86+
): Promise<DeleteConsumerGroupsResponse> {
87+
return this.service.deleteGroup({ instanceId }, dto);
88+
}
6689
}

redisinsight/api/src/modules/browser/dto/stream.dto.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@ export class CreateConsumerGroupsDto extends KeyDto {
258258
})
259259
@ValidateNested()
260260
@IsArray()
261+
@ArrayNotEmpty()
261262
@Type(() => CreateConsumerGroupDto)
262263
consumerGroups: CreateConsumerGroupDto[];
263264
}
@@ -266,3 +267,25 @@ export class UpdateConsumerGroupDto extends IntersectionType(
266267
KeyDto,
267268
CreateConsumerGroupDto,
268269
) {}
270+
271+
export class DeleteConsumerGroupsDto extends KeyDto {
272+
@ApiProperty({
273+
description: 'Consumer group names',
274+
type: String,
275+
isArray: true,
276+
example: ['Group-1', 'Group-1'],
277+
})
278+
@IsDefined()
279+
@IsArray()
280+
@ArrayNotEmpty()
281+
@Type(() => String)
282+
consumerGroups: string[];
283+
}
284+
285+
export class DeleteConsumerGroupsResponse {
286+
@ApiProperty({
287+
description: 'Number of deleted consumer groups',
288+
type: Number,
289+
})
290+
affected: number;
291+
}

redisinsight/api/src/modules/browser/services/stream/consumer-group.service.ts

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@ import {
1111
import { BrowserToolService } from 'src/modules/browser/services/browser-tool/browser-tool.service';
1212
import { KeyDto } from 'src/modules/browser/dto';
1313
import ERROR_MESSAGES from 'src/constants/error-messages';
14-
import {ConsumerGroupDto, CreateConsumerGroupsDto, UpdateConsumerGroupDto} from 'src/modules/browser/dto/stream.dto';
14+
import {
15+
ConsumerGroupDto,
16+
CreateConsumerGroupsDto,
17+
DeleteConsumerGroupsDto, DeleteConsumerGroupsResponse,
18+
UpdateConsumerGroupDto
19+
} from 'src/modules/browser/dto/stream.dto';
1520

1621
@Injectable()
1722
export class ConsumerGroupService {
@@ -196,6 +201,63 @@ export class ConsumerGroupService {
196201
}
197202
}
198203

204+
/**
205+
* Delete consumer groups in batch
206+
* @param clientOptions
207+
* @param dto
208+
*/
209+
async deleteGroup(
210+
clientOptions: IFindRedisClientInstanceByOptions,
211+
dto: DeleteConsumerGroupsDto,
212+
): Promise<DeleteConsumerGroupsResponse> {
213+
try {
214+
this.logger.log('Deleting consumer group.');
215+
216+
const exists = await this.browserTool.execCommand(
217+
clientOptions,
218+
BrowserToolKeysCommands.Exists,
219+
[dto.keyName],
220+
);
221+
222+
if (!exists) {
223+
return Promise.reject(new NotFoundException(ERROR_MESSAGES.KEY_NOT_EXIST));
224+
}
225+
226+
const toolCommands: Array<[
227+
toolCommand: BrowserToolCommands,
228+
...args: Array<string | number>,
229+
]> = dto.consumerGroups.map((group) => (
230+
[
231+
BrowserToolStreamCommands.XGroupDestroy,
232+
dto.keyName,
233+
group,
234+
]
235+
));
236+
237+
const [
238+
transactionError,
239+
transactionResults,
240+
] = await this.browserTool.execMulti(clientOptions, toolCommands);
241+
catchTransactionError(transactionError, transactionResults);
242+
243+
this.logger.log('Consumer group(s) successfully deleted.');
244+
245+
return {
246+
affected: toolCommands.length,
247+
};
248+
} catch (error) {
249+
if (error instanceof NotFoundException) {
250+
throw error;
251+
}
252+
253+
if (error?.message.includes(RedisErrorCodes.WrongType)) {
254+
throw new BadRequestException(error.message);
255+
}
256+
257+
throw catchAclError(error);
258+
}
259+
}
260+
199261
/**
200262
* Converts RESP response from Redis
201263
* [

0 commit comments

Comments
 (0)