Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions src/commands/apps/devices/forcechannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ export default defineCommand({
options: defineOptions(
z.object({
appId: z.string().uuid({ message: 'App ID must be a UUID.' }).optional().describe('ID of the app.'),
deviceId: z.string().optional().describe('ID of the device.'),
deviceId: z.array(z.string()).optional().describe('ID of the device. Can be specified multiple times.'),
channel: z.string().optional().describe('Name of the channel to force.'),
}),
),
action: withAuth(async (options, args) => {
let { appId, deviceId, channel } = options;
let { appId, deviceId: deviceIds, channel } = options;

if (!appId) {
if (!isInteractive()) {
Expand All @@ -28,14 +28,15 @@ export default defineCommand({
appId = await promptAppSelection(organizationId);
}

if (!deviceId) {
if (!deviceIds || deviceIds.length === 0) {
if (!isInteractive()) {
consola.error('You must provide the device ID when running in non-interactive environment.');
process.exit(1);
}
deviceId = await prompt('Enter the device ID:', {
const deviceId = await prompt('Enter the device ID:', {
type: 'text',
});
deviceIds = [deviceId];
}

if (!channel) {
Expand Down Expand Up @@ -65,11 +66,12 @@ export default defineCommand({
process.exit(1);
}

await appDevicesService.update({
await appDevicesService.updateMany({
appId,
deviceId,
deviceIds,
forcedAppChannelId: channelId,
});
consola.success('Device forced to channel successfully.');
const deviceCount = deviceIds.length;
consola.success(`${deviceCount === 1 ? 'Device' : `${deviceCount} devices`} forced to channel successfully.`);
}),
});
18 changes: 11 additions & 7 deletions src/commands/apps/devices/unforcechannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ export default defineCommand({
options: defineOptions(
z.object({
appId: z.string().uuid({ message: 'App ID must be a UUID.' }).optional().describe('ID of the app.'),
deviceId: z.string().optional().describe('ID of the device.'),
deviceId: z.array(z.string()).optional().describe('ID of the device. Can be specified multiple times.'),
}),
),
action: withAuth(async (options, args) => {
let { appId, deviceId } = options;
let { appId, deviceId: deviceIds } = options;

if (!appId) {
if (!isInteractive()) {
Expand All @@ -26,21 +26,25 @@ export default defineCommand({
appId = await promptAppSelection(organizationId);
}

if (!deviceId) {
if (!deviceIds || deviceIds.length === 0) {
if (!isInteractive()) {
consola.error('You must provide the device ID when running in non-interactive environment.');
process.exit(1);
}
deviceId = await prompt('Enter the device ID:', {
const deviceId = await prompt('Enter the device ID:', {
type: 'text',
});
deviceIds = [deviceId];
}

await appDevicesService.update({
await appDevicesService.updateMany({
appId,
deviceId,
deviceIds,
forcedAppChannelId: null,
});
consola.success('Forced channel removed from device successfully.');
const deviceCount = deviceIds.length;
consola.success(
`Forced channel removed from ${deviceCount === 1 ? 'device' : `${deviceCount} devices`} successfully.`,
);
}),
});
15 changes: 15 additions & 0 deletions src/services/app-devices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
ProbeAppDeviceDto,
ProbeAppDeviceResponseDto,
UpdateAppDeviceDto,
UpdateAppDevicesDto,
} from '@/types/index.js';
import httpClient, { HttpClient } from '@/utils/http-client.js';
import authorizationService from '@/services/authorization-service.js';
Expand All @@ -14,6 +15,7 @@ export interface AppDevicesService {
findOneById(dto: FindOneAppDeviceDto): Promise<AppDeviceDto>;
probe(dto: ProbeAppDeviceDto): Promise<ProbeAppDeviceResponseDto>;
update(dto: UpdateAppDeviceDto): Promise<void>;
updateMany(dto: UpdateAppDevicesDto): Promise<void>;
}

class AppDevicesServiceImpl implements AppDevicesService {
Expand Down Expand Up @@ -80,6 +82,19 @@ class AppDevicesServiceImpl implements AppDevicesService {
},
);
}

async updateMany(data: UpdateAppDevicesDto): Promise<void> {
const ids = data.deviceIds.join(',');
await this.httpClient.patch(
`/v1/apps/${data.appId}/devices?ids=${ids}`,
{ forcedAppChannelId: data.forcedAppChannelId },
Comment thread
robingenz marked this conversation as resolved.
{
headers: {
Authorization: `Bearer ${authorizationService.getCurrentAuthorizationToken()}`,
},
},
);
}
}

const appDevicesService: AppDevicesService = new AppDevicesServiceImpl(httpClient);
Expand Down
6 changes: 6 additions & 0 deletions src/types/app-device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,9 @@ export interface UpdateAppDeviceDto {
deviceId: string;
forcedAppChannelId?: string | null;
}

export interface UpdateAppDevicesDto {
appId: string;
deviceIds: string[];
forcedAppChannelId?: string | null;
}
Loading