Skip to content

Commit c9a55e5

Browse files
committed
api/delete device
1 parent af0d448 commit c9a55e5

File tree

6 files changed

+70
-7
lines changed

6 files changed

+70
-7
lines changed

src/modules/authenticate/authenticate.module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { ConfigModule, ConfigService } from "@nestjs/config";
2323
imports: [ConfigModule],
2424
useFactory: async (configService: ConfigService) => ({
2525
secret: configService.get<string>("JWT_SECRET"),
26-
signOptions: { expiresIn: "60m" },
26+
signOptions: { expiresIn: "2h" },
2727
}),
2828
inject: [ConfigService],
2929
}),

src/modules/authenticate/authenticate.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ export class AuthenticateService {
9696
},
9797
{
9898
secret: this.configService.get("JWT_SECRET"),
99-
expiresIn: "1h",
99+
expiresIn: "2h",
100100
},
101101
),
102102
this.jwtService.signAsync(

src/modules/device/device.resolver.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { UseGuards } from "@nestjs/common";
22
import { Args, Mutation, Query, Resolver } from "@nestjs/graphql";
33
import { JwtAuthGuard } from "../../guards/auth/auth.guard";
44
import { DeviceService } from "./device.service";
5-
import { CreateDeviceInput, UpdateItemInput, CreateDevicesInput } from "./dto/input.dto";
5+
import { CreateDeviceInput, UpdateItemInput, CreateDevicesInput, DeleteDeviceInput } from "./dto/input.dto";
66
import { DeviceQueryInput } from "./dto/query.dto";
77
import { DevicesResponse } from "./dto/response.dto";
88
import { DeviceResponse } from "./entities/device.entity";
@@ -37,6 +37,12 @@ export class DeviceResolver {
3737
return this.deviceService.update(input);
3838
}
3939

40+
@Mutation(() => String)
41+
@UseGuards(JwtAuthGuard)
42+
async delete_device(@Args("input") input: DeleteDeviceInput) {
43+
return this.deviceService.delete(input);
44+
}
45+
4046
@Query(() => DevicesResponse)
4147
async devices_by_license(@Args("license") license: string) {
4248
const data = await this.deviceService.findDevicesByLicense(license);

src/modules/device/device.service.ts

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,61 @@
11
import { Injectable } from "@nestjs/common";
22
import { GraphQLError } from "graphql";
3-
import { CreateDeviceInput, UpdateItemInput, CreateDevicesInput } from "./dto/input.dto";
3+
import { CreateDeviceInput, UpdateItemInput, CreateDevicesInput, DeleteDeviceInput } from "./dto/input.dto";
44
import { DeviceQueryInput } from "./dto/query.dto";
55
import { PrismaService } from "../prisma/prisma.service";
66

77
@Injectable()
88
export class DeviceService {
9+
private readonly esp32s3Pin = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 21, 22, 23, 25, 26, 27, 32, 33, 34, 35, 36, 39];
10+
private readonly esp32DevKitPin = [2, 4, 12, 13, 14, 15, 16, 17, 18, 19, 21, 22, 23, 25, 26, 27, 32, 33];
11+
private readonly esp32c6Pin = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40];
912
constructor(private prisma: PrismaService) {}
1013

1114
async create(input: CreateDeviceInput) {
1215
try {
16+
var pin: number;
17+
if (input.protocol === "BLE") {
18+
const listBLEDevices = await this.prisma.device.findMany({
19+
where: {
20+
protocol: "BLE",
21+
},
22+
});
23+
this.esp32DevKitPin.forEach((element) => {
24+
if (!listBLEDevices.find((device) => device.pin === element)) {
25+
pin = element;
26+
}
27+
});
28+
} else if (input.protocol === "MQTT") {
29+
const listMQTTDevices = await this.prisma.device.findMany({
30+
where: {
31+
protocol: "MQTT",
32+
},
33+
});
34+
this.esp32s3Pin.forEach((element) => {
35+
if (!listMQTTDevices.find((device) => device.pin === element)) {
36+
pin = element;
37+
}
38+
});
39+
} else {
40+
const listDevices = await this.prisma.device.findMany({
41+
where: {
42+
protocol: "ZIGBEE",
43+
},
44+
});
45+
this.esp32c6Pin.forEach((element) => {
46+
if (!listDevices.find((device) => device.pin === element)) {
47+
pin = element;
48+
}
49+
});
50+
}
51+
1352
const data = await this.prisma.device.create({
1453
data: {
1554
device_name: input.device_name,
1655
current_state: input.current_state,
1756
userID: input.userID,
1857
protocol: input.protocol,
19-
pin: input.pin,
58+
pin: pin,
2059
},
2160
});
2261
return data;
@@ -96,4 +135,15 @@ export class DeviceService {
96135
throw new GraphQLError(err);
97136
}
98137
}
138+
139+
async delete(input: DeleteDeviceInput) {
140+
try {
141+
const data = await this.prisma.device.delete({
142+
where: { id: input.id },
143+
});
144+
return "Device deleted successfully";
145+
} catch (err) {
146+
throw new GraphQLError(err);
147+
}
148+
}
99149
}

src/modules/device/dto/input.dto.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export class CreateDeviceInput {
2020
@IsString()
2121
protocol: string;
2222

23-
@Field(() => Number, { nullable: false })
23+
@Field(() => Number, { nullable: true })
2424
@IsNumber()
2525
pin: number;
2626
}
@@ -36,6 +36,13 @@ export class UpdateItemInput {
3636
current_state: number;
3737
}
3838

39+
@InputType()
40+
export class DeleteDeviceInput {
41+
@Field(() => Number, { nullable: false })
42+
@IsNumber()
43+
id: number;
44+
}
45+
3946
@InputType()
4047
export class CreateDevicesInput {
4148
@Field(() => [CreateDeviceInput])

src/modules/device/entities/device.entity.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export class DeviceResponse {
2323
@IsString()
2424
protocol: string;
2525

26-
@Field(() => Number, { nullable: false })
26+
@Field(() => Number, { nullable: true })
2727
@IsNumber()
2828
pin: number;
2929

0 commit comments

Comments
 (0)