Skip to content

Commit a161ebf

Browse files
committed
feat: update Hosts model to support excludedInternalSquads
- Bumped version to 2.3.16 in package.json. - Added excludedInternalSquads field to Hosts model and related schemas. - Enhanced create and update host commands to handle excludedInternalSquads. - Updated service methods to manage excluded internal squads for hosts. - Refactored response models to include excludedInternalSquads data. - Introduced repository methods for adding and clearing excluded internal squads.
1 parent c35ace5 commit a161ebf

File tree

16 files changed

+154
-292
lines changed

16 files changed

+154
-292
lines changed

libs/contract/commands/hosts/create.command.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ export namespace CreateHostCommand {
7878
mihomoX25519: z.optional(z.boolean().default(false)),
7979
nodes: z.optional(z.array(z.string().uuid())),
8080
xrayJsonTemplateUuid: z.optional(z.string().uuid().nullable()),
81+
excludedInternalSquads: z
82+
.optional(z.array(z.string().uuid()))
83+
.describe('Optional. Internal squads from which the host will be excluded.'),
8184
});
8285

8386
export type Request = z.infer<typeof RequestSchema>;

libs/contract/commands/hosts/update.command.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ export namespace UpdateHostCommand {
8282
mihomoX25519: z.optional(z.boolean()),
8383
nodes: z.optional(z.array(z.string().uuid())),
8484
xrayJsonTemplateUuid: z.optional(z.string().uuid().nullable()),
85+
excludedInternalSquads: z
86+
.optional(z.array(z.string().uuid()))
87+
.describe('Optional. Internal squads from which the host will be excluded.'),
8588
});
8689
export type Request = z.infer<typeof RequestSchema>;
8790

libs/contract/models/hosts.schema.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,5 @@ export const HostsSchema = z.object({
3535

3636
nodes: z.array(z.string().uuid()),
3737
xrayJsonTemplateUuid: z.string().uuid().nullable(),
38+
excludedInternalSquads: z.array(z.string().uuid()),
3839
});

libs/contract/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@remnawave/backend-contract",
3-
"version": "2.3.14",
3+
"version": "2.3.16",
44
"public": true,
55
"license": "AGPL-3.0-only",
66
"description": "A contract library for Remnawave Backend. It can be used in backend and frontend.",
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
-- CreateTable
2+
CREATE TABLE "public"."internal_squad_host_exclusions" (
3+
"host_uuid" UUID NOT NULL,
4+
"squad_uuid" UUID NOT NULL,
5+
6+
CONSTRAINT "internal_squad_host_exclusions_pkey" PRIMARY KEY ("host_uuid","squad_uuid")
7+
);
8+
9+
-- AddForeignKey
10+
ALTER TABLE "public"."internal_squad_host_exclusions" ADD CONSTRAINT "internal_squad_host_exclusions_host_uuid_fkey" FOREIGN KEY ("host_uuid") REFERENCES "public"."hosts"("uuid") ON DELETE CASCADE ON UPDATE CASCADE;
11+
12+
-- AddForeignKey
13+
ALTER TABLE "public"."internal_squad_host_exclusions" ADD CONSTRAINT "internal_squad_host_exclusions_squad_uuid_fkey" FOREIGN KEY ("squad_uuid") REFERENCES "public"."internal_squads"("uuid") ON DELETE CASCADE ON UPDATE CASCADE;

prisma/schema.prisma

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,11 +315,23 @@ model Hosts {
315315
configProfiles ConfigProfiles? @relation(fields: [configProfileUuid], references: [uuid], onDelete: SetNull)
316316
xrayJsonTemplate SubscriptionTemplate? @relation(fields: [xrayJsonTemplateUuid], references: [uuid], onDelete: SetNull)
317317
318-
nodes HostsToNodes[]
318+
nodes HostsToNodes[]
319+
excludedInternalSquads InternalSquadHostExclusions[]
319320
320321
@@map("hosts")
321322
}
322323

324+
model InternalSquadHostExclusions {
325+
hostUuid String @map("host_uuid") @db.Uuid
326+
squadUuid String @map("squad_uuid") @db.Uuid
327+
328+
host Hosts @relation(fields: [hostUuid], references: [uuid], onDelete: Cascade)
329+
squad InternalSquads @relation(fields: [squadUuid], references: [uuid], onDelete: Cascade)
330+
331+
@@id([hostUuid, squadUuid])
332+
@@map("internal_squad_host_exclusions")
333+
}
334+
323335
model SubscriptionTemplate {
324336
uuid String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
325337
viewPosition Int @default(autoincrement()) @map("view_position")
@@ -390,6 +402,7 @@ model InternalSquads {
390402
391403
internalSquadMembers InternalSquadMembers[]
392404
internalSquadInbounds InternalSquadInbounds[]
405+
excludedHosts InternalSquadHostExclusions[]
393406
394407
@@map("internal_squads")
395408
}

src/modules/hosts/controllers/hosts-bulk-actions.controller.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ import {
1414
SetInboundToManyHostsCommand,
1515
SetPortToManyHostsCommand,
1616
} from '@libs/contracts/commands';
17-
import { HOSTS_CONTROLLER } from '@libs/contracts/api/controllers';
18-
import { CONTROLLERS_INFO } from '@libs/contracts/api';
17+
import { CONTROLLERS_INFO, HOSTS_CONTROLLER } from '@libs/contracts/api';
1918
import { ROLE } from '@libs/contracts/constants';
2019

2120
import {
@@ -30,8 +29,8 @@ import {
3029
SetPortToManyHostsRequestDto,
3130
SetPortToManyHostsResponseDto,
3231
} from '../dtos/bulk-operations.dto';
33-
import { GetAllHostsResponseModel } from '../models/get-all-hosts.response.model';
3432
import { HostsService } from '../hosts.service';
33+
import { HostResponseModel } from '../models';
3534

3635
@ApiBearerAuth('Authorization')
3736
@ApiTags(CONTROLLERS_INFO.HOSTS_BULK_ACTIONS.tag)
@@ -57,7 +56,7 @@ export class HostsBulkActionsController {
5756

5857
const data = errorHandler(result);
5958
return {
60-
response: data.map((host) => new GetAllHostsResponseModel(host)),
59+
response: data.map((host) => new HostResponseModel(host)),
6160
};
6261
}
6362

@@ -76,7 +75,7 @@ export class HostsBulkActionsController {
7675

7776
const data = errorHandler(result);
7877
return {
79-
response: data.map((host) => new GetAllHostsResponseModel(host)),
78+
response: data.map((host) => new HostResponseModel(host)),
8079
};
8180
}
8281

@@ -95,7 +94,7 @@ export class HostsBulkActionsController {
9594

9695
const data = errorHandler(result);
9796
return {
98-
response: data.map((host) => new GetAllHostsResponseModel(host)),
97+
response: data.map((host) => new HostResponseModel(host)),
9998
};
10099
}
101100

@@ -118,7 +117,7 @@ export class HostsBulkActionsController {
118117

119118
const data = errorHandler(result);
120119
return {
121-
response: data.map((host) => new GetAllHostsResponseModel(host)),
120+
response: data.map((host) => new HostResponseModel(host)),
122121
};
123122
}
124123

@@ -137,7 +136,7 @@ export class HostsBulkActionsController {
137136

138137
const data = errorHandler(result);
139138
return {
140-
response: data.map((host) => new GetAllHostsResponseModel(host)),
139+
response: data.map((host) => new HostResponseModel(host)),
141140
};
142141
}
143142
}

src/modules/hosts/controllers/hosts.controller.ts

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,24 @@ import {
2323
ReorderHostCommand,
2424
UpdateHostCommand,
2525
} from '@libs/contracts/commands';
26-
import { HOSTS_CONTROLLER } from '@libs/contracts/api/controllers';
27-
import { CONTROLLERS_INFO } from '@libs/contracts/api';
26+
import { CONTROLLERS_INFO, HOSTS_CONTROLLER } from '@libs/contracts/api';
2827
import { ROLE } from '@libs/contracts/constants';
2928

30-
import { ReorderHostRequestDto, ReorderHostResponseDto } from '../dtos/reorder-hosts.dto';
31-
import { CreateHostRequestDto, CreateHostResponseDto } from '../dtos/create-host.dto';
32-
import { DeleteHostRequestDto, DeleteHostResponseDto } from '../dtos/delete-host.dto';
33-
import { GetAllHostTagsResponseModel } from '../models/get-all-tags.response.model';
34-
import { GetAllHostsResponseModel } from '../models/get-all-hosts.response.model';
35-
import { GetOneHostResponseModel } from '../models/get-one-host.response.model';
36-
import { CreateHostResponseModel } from '../models/create-host.response.model';
37-
import { UpdateHostResponseModel } from '../models/update-host.response.model';
38-
import { GetAllHostTagsResponseDto } from '../dtos/get-all-host-tags.dto';
39-
import { GetAllHostsResponseDto } from '../dtos/get-all-hosts.dto';
40-
import { UpdateHostResponseDto } from '../dtos/update-host.dto';
41-
import { UpdateHostRequestDto } from '../dtos/update-host.dto';
42-
import { GetOneHostResponseDto } from '../dtos/get-one.dto';
43-
import { GetOneHostRequestDto } from '../dtos/get-one.dto';
29+
import {
30+
ReorderHostRequestDto,
31+
ReorderHostResponseDto,
32+
GetAllHostTagsResponseDto,
33+
CreateHostResponseDto,
34+
CreateHostRequestDto,
35+
DeleteHostRequestDto,
36+
DeleteHostResponseDto,
37+
GetAllHostsResponseDto,
38+
UpdateHostResponseDto,
39+
UpdateHostRequestDto,
40+
GetOneHostResponseDto,
41+
GetOneHostRequestDto,
42+
} from '../dtos';
43+
import { GetAllHostTagsResponseModel, HostResponseModel } from '../models';
4444
import { HostsService } from '../hosts.service';
4545

4646
@ApiBearerAuth('Authorization')
@@ -83,7 +83,7 @@ export class HostsController {
8383

8484
const data = errorHandler(result);
8585
return {
86-
response: new CreateHostResponseModel(data),
86+
response: new HostResponseModel(data),
8787
};
8888
}
8989

@@ -101,7 +101,7 @@ export class HostsController {
101101

102102
const data = errorHandler(result);
103103
return {
104-
response: new UpdateHostResponseModel(data),
104+
response: new HostResponseModel(data),
105105
};
106106
}
107107

@@ -118,7 +118,7 @@ export class HostsController {
118118

119119
const data = errorHandler(result);
120120
return {
121-
response: data.map((host) => new GetAllHostsResponseModel(host)),
121+
response: data.map((host) => new HostResponseModel(host)),
122122
};
123123
}
124124

@@ -136,7 +136,7 @@ export class HostsController {
136136

137137
const data = errorHandler(result);
138138
return {
139-
response: new GetOneHostResponseModel(data),
139+
response: new HostResponseModel(data),
140140
};
141141
}
142142

src/modules/hosts/entities/hosts.entity.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ export class HostsEntity implements Hosts {
3838
nodeUuid: string;
3939
}[];
4040

41+
excludedInternalSquads: {
42+
squadUuid: string;
43+
}[];
44+
4145
constructor(data: Partial<Hosts>) {
4246
Object.assign(this, data);
4347
}

src/modules/hosts/hosts.service.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ export class HostsService {
8888
serverDescription = undefined;
8989
}
9090

91-
const { inbound: inboundObj, nodes, ...rest } = dto;
91+
const { inbound: inboundObj, nodes, excludedInternalSquads, ...rest } = dto;
9292

9393
const configProfile = await this.queryBus.execute(
9494
new GetConfigProfileByUuidQuery(inboundObj.configProfileUuid),
@@ -134,6 +134,18 @@ export class HostsService {
134134
});
135135
}
136136

137+
if (excludedInternalSquads !== undefined && excludedInternalSquads.length > 0) {
138+
await this.hostsRepository.addExcludedInternalSquadsToHost(
139+
result.uuid,
140+
excludedInternalSquads,
141+
);
142+
result.excludedInternalSquads = excludedInternalSquads.map((squad) => {
143+
return {
144+
squadUuid: squad,
145+
};
146+
});
147+
}
148+
137149
return {
138150
isOk: true,
139151
response: result,
@@ -147,7 +159,7 @@ export class HostsService {
147159

148160
public async updateHost(dto: UpdateHostRequestDto): Promise<ICommandResponse<HostsEntity>> {
149161
try {
150-
const { inbound: inboundObj, nodes, ...rest } = dto;
162+
const { inbound: inboundObj, nodes, excludedInternalSquads, ...rest } = dto;
151163

152164
const host = await this.hostsRepository.findByUUID(dto.uuid);
153165
if (!host) {
@@ -255,6 +267,14 @@ export class HostsService {
255267
await this.hostsRepository.addNodesToHost(host.uuid, nodes);
256268
}
257269

270+
if (excludedInternalSquads !== undefined) {
271+
await this.hostsRepository.clearExcludedInternalSquadsFromHost(host.uuid);
272+
await this.hostsRepository.addExcludedInternalSquadsToHost(
273+
host.uuid,
274+
excludedInternalSquads,
275+
);
276+
}
277+
258278
const result = await this.hostsRepository.update({
259279
...rest,
260280
address: dto.address ? dto.address.trim() : undefined,

0 commit comments

Comments
 (0)