Skip to content

Commit e6649d8

Browse files
authored
Merge pull request #1170 from david-roper/subject-cli-commands
Subject cli commands
2 parents 441bd8a + 03d408d commit e6649d8

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-2
lines changed

apps/api/src/subjects/subjects.controller.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { CurrentUser, ParseSchemaPipe, RouteAccess } from '@douglasneuroinformat
33
import type { AppAbility } from '@douglasneuroinformatics/libnest';
44
import { Body, Controller, Delete, Get, Param, Post, Query } from '@nestjs/common';
55
import { ApiOperation, ApiTags } from '@nestjs/swagger';
6+
import z from 'zod/v4';
67

78
import { CreateSubjectDto } from './dto/create-subject.dto';
89
import { SubjectsService } from './subjects.service';
@@ -23,7 +24,8 @@ export class SubjectsController {
2324
@Delete(':id')
2425
@RouteAccess({ action: 'delete', subject: 'Subject' })
2526
deleteById(
26-
@Param('id') id: string,
27+
@Param('id', new ParseSchemaPipe({ schema: z.string().transform((value) => decodeURIComponent(value)) }))
28+
id: string,
2729
@Query('force', new ParseSchemaPipe({ isOptional: true, schema: $BooleanLike })) force: boolean | undefined,
2830
@CurrentUser('ability') ability: AppAbility
2931
) {

cli/odc-cli

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ from argparse import (
2222
from datetime import datetime
2323
from typing import Any, Callable, TypedDict
2424
from urllib.error import HTTPError, URLError
25-
from urllib.parse import parse_qsl, urlencode, urlparse, urlunparse
25+
from urllib.parse import parse_qsl, urlencode, urlparse, urlunparse, quote
2626
from urllib.request import Request, urlopen
2727
from urllib.response import addinfourl
2828

@@ -41,6 +41,7 @@ Features:
4141
- Store and retrieve configuration (e.g., base URL)
4242
- Retrieve a list of instruments in the database
4343
- Update and delete instrument records
44+
- Find and delete subjects
4445
- Get the current status of an ODC instance
4546
4647
Requires:
@@ -382,6 +383,39 @@ class InstrumentRecordsCommands:
382383
print(response)
383384

384385

386+
class SubjectCommands:
387+
@require_url
388+
@require_token
389+
@staticmethod
390+
def delete(id: str) -> None:
391+
entry = input("Are you sure you want to delete this subject? Type DELETE to Confirm: ")
392+
if entry == "DELETE":
393+
url =build_url_with_params(
394+
f'{config.base_url}/v1/subjects/{quote(id, safe="")}',
395+
{
396+
'force': True
397+
}
398+
)
399+
response = HttpClient.delete(url)
400+
print(response)
401+
else:
402+
print("Cancelling delete command")
403+
404+
@require_url
405+
@require_token
406+
@staticmethod
407+
def find(min_date: datetime | None, subject_id: str | None) -> None:
408+
url = build_url_with_params(
409+
f'{config.base_url}/v1/subjects',
410+
{
411+
'minDate': min_date,
412+
'subjectId': subject_id,
413+
},
414+
)
415+
response = HttpClient.get(url)
416+
print(response)
417+
418+
385419
class InstrumentsCommands:
386420
@require_url
387421
@require_token
@@ -453,6 +487,7 @@ class CLI:
453487
self._create_config_parser()
454488
self._create_instrument_records_parser()
455489
self._create_instruments_parser()
490+
self._create_subjects_parser()
456491
self._create_setup_parser()
457492

458493
def run(self) -> None:
@@ -517,6 +552,22 @@ class CLI:
517552
)
518553
update_parser.set_defaults(fn=InstrumentRecordsCommands.update)
519554

555+
def _create_subjects_parser(self):
556+
subparsers = self._create_subcommand_subparsers('subjects')
557+
558+
delete_parser = subparsers.add_parser('delete', help='delete an subject record by id')
559+
delete_parser.add_argument('--id', required=True, help='id of the subject record to delete')
560+
delete_parser.set_defaults(fn=SubjectCommands.delete)
561+
562+
find_parser = subparsers.add_parser('find', help='search subject records by optional filters')
563+
find_parser.add_argument(
564+
'--min-date',
565+
type=ArgumentTypes.valid_datetime,
566+
help='filter records created after this date (format: yyyy-mm-dd or yyyy-mm-dd hh:mm:ss)',
567+
)
568+
find_parser.add_argument('--subject-id', help='filter by subject id')
569+
find_parser.set_defaults(fn=SubjectCommands.find)
570+
520571
def _create_instruments_parser(self):
521572
subparsers = self._create_subcommand_subparsers('instruments')
522573
get_parser = subparsers.add_parser('list', help='list all instruments, optionally filtered by kind')

0 commit comments

Comments
 (0)