Skip to content

Commit 23b30f3

Browse files
Merge pull request #9 from neuralarchitects/shareDevice
Fixed the shared device issues
2 parents 7cb40b3 + 5d49d01 commit 23b30f3

File tree

13 files changed

+482
-236
lines changed

13 files changed

+482
-236
lines changed

admin_web_app/Source_webapp/src/services/DeviceApi.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,8 @@ export async function apiGetAllDevices<T>() {
1818

1919
export async function apiUnshareDevice<T>(deviceId: string) {
2020
return ApiService.fetchData<T>({
21-
url: `${import.meta.env.VITE_URL}v1/device/edit`,
21+
url: `${import.meta.env.VITE_URL}v1/device/global-unshare/${deviceId}`,
2222
method: 'patch',
23-
data: {
24-
deviceId: deviceId,
25-
isShared: false,
26-
},
2723
})
2824
}
2925

admin_web_app/Source_webapp/src/views/alldevices/components/DeviceTable.tsx

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -98,18 +98,30 @@ const DevicesTable: React.FC<UsersTableProps> = ({ setCount, superAdmin }) => {
9898
try {
9999
setApiLoading(true)
100100
const res = await apiUnshareDevice(String(deviceData?._id))
101-
setApiLoading(false)
102-
setUnshareDialog(false)
103-
toast.push(
104-
<Notification
105-
title={'Device was successfully unshared.'}
106-
type="success"
107-
/>,
108-
{
109-
placement: 'top-center',
110-
}
111-
)
112-
refreshPage()
101+
if (res.status !== 200) {
102+
toast.push(
103+
<Notification
104+
title={'Error while unsharing device'}
105+
type="danger"
106+
/>,
107+
{
108+
placement: 'top-center',
109+
}
110+
)
111+
} else {
112+
setApiLoading(false)
113+
setUnshareDialog(false)
114+
toast.push(
115+
<Notification
116+
title={'Device was successfully unshared.'}
117+
type="success"
118+
/>,
119+
{
120+
placement: 'top-center',
121+
}
122+
)
123+
refreshPage()
124+
}
113125
} catch (error: any) {
114126
setApiLoading(false)
115127
setUnshareDialog(false)

backend/src/modules/device/controllers/device.controller.ts

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import { DeviceService } from '../services/device.service';
3232
import { EditDeviceDto } from '../data-transfer-objects/edit-device.dto';
3333
import { UserService } from 'src/modules/user/services/user/user.service';
3434
import { LocalShareDto } from '../data-transfer-objects/local-share-dto';
35+
import { GlobalShareDto } from '../data-transfer-objects/global-share-dto';
3536

3637
@ApiTags('Manage Devices')
3738
@Controller('app')
@@ -142,6 +143,50 @@ export class DeviceController {
142143
return this.result;
143144
}
144145

146+
@Patch('v1/device/global-unshare/:deviceId')
147+
@HttpCode(200)
148+
@UseGuards(JwtAuthGuard)
149+
@ApiBearerAuth()
150+
@ApiOperation({
151+
summary: 'Global unshare device.',
152+
description: 'This API will globally unshare a device with user',
153+
})
154+
async globalUnShareDevice(
155+
@Param('deviceId') deviceId: string,
156+
@Request() request,
157+
) {
158+
const isAdmin = await this.isAdmin(request.user.userId);
159+
await this.deviceService.unshareGlobalDevice(
160+
deviceId,
161+
request.user.userId,
162+
isAdmin,
163+
);
164+
return { message: 'Device unshared' };
165+
}
166+
167+
@Patch('v1/device/global-share/:deviceId')
168+
@HttpCode(200)
169+
@UseGuards(JwtAuthGuard)
170+
@ApiBearerAuth()
171+
@ApiOperation({
172+
summary: 'Global share device.',
173+
description: 'This API will globally share a device with user',
174+
})
175+
async globalShareDevice(
176+
@Param('deviceId') deviceId: string,
177+
@Body() body: GlobalShareDto,
178+
@Request() request,
179+
) {
180+
const isAdmin = await this.isAdmin(request.user.userId);
181+
await this.deviceService.globalShareDevice(
182+
deviceId,
183+
body,
184+
request.user.userId,
185+
isAdmin,
186+
);
187+
return { message: 'Device sharing updated successfully' };
188+
}
189+
145190
@Get('v1/device/get-devices-by-user-id/:userId')
146191
@HttpCode(200)
147192
@UseGuards(JwtAuthGuard)
@@ -651,11 +696,11 @@ export class DeviceController {
651696
description:
652697
'This API will return all devices that are sharing with a user.',
653698
})
654-
async getAllDevicesSharingWithUser(
655-
@Request() request,
656-
) {
699+
async getAllDevicesSharingWithUser(@Request() request) {
657700
console.log('We are in getAllDevicesSharingWithUser controller');
658701

659-
return await this.deviceService.getSharedDevicesWithUserId(request.user.userId);
702+
return await this.deviceService.getSharedDevicesWithUserId(
703+
request.user.userId,
704+
);
660705
}
661706
}

backend/src/modules/device/data-transfer-objects/edit-device.dto.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,6 @@ export class EditDeviceDto {
6363
@ApiProperty({ required: false })
6464
parameters: [];
6565

66-
@IsOptional()
67-
@IsBoolean({ message: 'isShared must be boolean.' })
68-
@ApiProperty({ required: false })
69-
isShared: boolean;
70-
7166
@IsOptional()
7267
@IsNumber({}, { message: 'costOfUse must be number.' })
7368
@ApiProperty({ required: false })
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { ApiProperty } from '@nestjs/swagger';
2+
import {
3+
ArrayMaxSize,
4+
ArrayMinSize,
5+
IsArray,
6+
IsNumber,
7+
IsOptional,
8+
} from 'class-validator';
9+
10+
export class GlobalShareDto {
11+
@IsOptional()
12+
@IsNumber({}, { message: 'costOfUse must be number.' })
13+
@ApiProperty({ required: false })
14+
costOfUse: number;
15+
16+
@IsArray()
17+
@ArrayMinSize(2)
18+
@ArrayMaxSize(2)
19+
@ApiProperty({
20+
required: true,
21+
type: [Number],
22+
description: 'Array of [latitude, longitude]',
23+
})
24+
coordinate: number[];
25+
}

backend/src/modules/device/interfaces/device.interface.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Types } from 'mongoose';
22
import { Document } from 'mongoose';
33

44
export interface Device extends Document {
5-
nodeMqttAddress?: string
5+
nodeMqttAddress?: string;
66
userId: string;
77
nodeId: string;
88
nodeDeviceId: string;
@@ -17,7 +17,7 @@ export interface Device extends Document {
1717
sharedWith: Array<Types.ObjectId>;
1818
isShared: boolean;
1919
costOfUse: number;
20-
location: string;
20+
location: { coordinates: number[]; type: string } | string;
2121
geometry: string;
2222
insertedBy: string;
2323
insertDate: string;

backend/src/modules/device/repositories/device.repository.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,7 @@ export class DeviceRepository {
9191
.select(this.getDeviceKeys());
9292
}
9393

94-
async getDevicesByUserId(
95-
userId,
96-
) {
94+
async getDevicesByUserId(userId) {
9795
console.log('we are in getDevicesByUserId repository!');
9896

9997
return await this.deviceModel
@@ -286,6 +284,4 @@ export class DeviceRepository {
286284

287285
return !!result; // Returns true if found, false otherwise
288286
}
289-
290-
291287
}

0 commit comments

Comments
 (0)