fix: link new devices to all admin users in Traccar

When creating a device, automatically link it to all Traccar admin users
so they can see it regardless of which account created the device.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-02-02 22:59:13 +01:00
parent cbba5d40b8
commit 651f4d2aa8

View File

@@ -180,12 +180,55 @@ export class TraccarClientService implements OnModuleInit {
* Create a new device in Traccar
*/
async createDevice(name: string, uniqueId: string, phone?: string): Promise<TraccarDevice> {
return this.request('post', '/api/devices', {
const device = await this.request<TraccarDevice>('post', '/api/devices', {
name,
uniqueId,
phone,
category: 'person',
});
// Link device to all admin users so they can see it
await this.linkDeviceToAllAdmins(device.id);
return device;
}
/**
* Link a device to a specific user
*/
async linkDeviceToUser(deviceId: number, userId: number): Promise<boolean> {
try {
await this.request('post', '/api/permissions', {
userId,
deviceId,
});
return true;
} catch (error: any) {
// 400 means permission already exists, which is fine
if (error.response?.status === 400) {
return true;
}
this.logger.warn(`Failed to link device ${deviceId} to user ${userId}: ${error.message}`);
return false;
}
}
/**
* Link a device to all admin users
*/
async linkDeviceToAllAdmins(deviceId: number): Promise<void> {
try {
const users = await this.getAllUsers();
const admins = users.filter(u => u.administrator);
for (const admin of admins) {
await this.linkDeviceToUser(deviceId, admin.id);
}
this.logger.log(`Linked device ${deviceId} to ${admins.length} admin users`);
} catch (error: any) {
this.logger.warn(`Failed to link device to admins: ${error.message}`);
}
}
/**