package Ġuk . gov . just ice . digital . h mp ps . h mp ps inter vention ss ervice . controller Ċ Ċ import Ġorg . springframework . http . HttpStatus Ċ import Ġorg . springframework . http . Response Entity Ċ import Ġorg . springframework . security . oauth 2 . server . resource . authentication . Jwt Authentication Token Ċ import Ġorg . springframework . web . bind . annotation . GetMapping Ċ import Ġorg . springframework . web . bind . annotation . Patch Mapping Ċ import Ġorg . springframework . web . bind . annotation . PathVariable Ċ import Ġorg . springframework . web . bind . annotation . PostMapping Ċ import Ġorg . springframework . web . bind . annotation . RequestBody Ċ import Ġorg . springframework . web . bind . annotation . RequestParam Ċ import Ġorg . springframework . web . bind . annotation . RestController Ċ import Ġorg . springframework . web . server . ResponseStatus Exception Ċ import Ġorg . springframework . web . server . Server Web Input Exception Ċ import Ġorg . springframework . web . servlet . support . Servlet Uri Components Builder Ċ import Ġuk . gov . just ice . digital . h mp ps . h mp ps inter vention ss ervice . controller . map pers . Jwt Auth User Mapper Ċ import Ġuk . gov . just ice . digital . h mp ps . h mp ps inter vention ss ervice . dto . Cancellation Re asons DTO Ċ import Ġuk . gov . just ice . digital . h mp ps . h mp ps inter vention ss ervice . dto . Create Ref erral Request DTO Ċ import Ġuk . gov . just ice . digital . h mp ps . h mp ps inter vention ss ervice . dto . Draft Ref erral DTO Ċ import Ġuk . gov . just ice . digital . h mp ps . h mp ps inter vention ss ervice . dto . Ended Ref erral DTO Ċ import Ġuk . gov . just ice . digital . h mp ps . h mp ps inter vention ss ervice . dto . Ref erral Assignment DTO Ċ import Ġuk . gov . just ice . digital . h mp ps . h mp ps inter vention ss ervice . dto . Sent Ref erral DTO Ċ import Ġuk . gov . just ice . digital . h mp ps . h mp ps inter vention ss ervice . dto . Service Category DTO Ċ import Ġuk . gov . just ice . digital . h mp ps . h mp ps inter vention ss ervice . jpa . entity . Auth User Ċ import Ġuk . gov . just ice . digital . h mp ps . h mp ps inter vention ss ervice . service . H MP PS Auth Service Ċ import Ġuk . gov . just ice . digital . h mp ps . h mp ps inter vention ss ervice . service . Ref erral Service Ċ import Ġuk . gov . just ice . digital . h mp ps . h mp ps inter vention ss ervice . service . Service Category Service Ċ import Ġjava . util . UUID Ċ import Ġjavax . persistence . Entity NotFoundException Ċ Ċ @ RestController Ċ class ĠRef erral Controller ( ĊĠ Ġprivate Ġval Ġref erral Service : ĠRef erral Service , ĊĠ Ġprivate Ġval Ġservice Category Service : ĠService Category Service , ĊĠ Ġprivate Ġval Ġh mp ps Auth Service : ĠH MP PS Auth Service , ĊĠ Ġval Ġjwt Auth User Mapper : ĠJwt Auth User Mapper , Ċ ) Ġ{ ĊĠ Ġ@ PostMapping ("/ sent - ref erral /{ id }/ assign ") ĊĠ Ġfun Ġassign Sent Ref erral ( ĊĠĠĠ Ġ@ PathVariable Ġid : ĠUUID , ĊĠĠĠ Ġ@ RequestBody Ġref erral Assignment : ĠRef erral Assignment DTO , ĊĠĠĠ Ġauthentication : ĠJwt Authentication Token , ĊĠ Ġ): ĠSent Ref erral DTO Ġ{ ĊĠĠĠ Ġval Ġsent Ref erral Ġ= Ġref erral Service . get Sent Ref erral ( id ) ĊĠĠĠĠĠ Ġ?: Ġthrow ĠResponse Status Exception ( HttpStatus . NOT _ FOUND , Ġ" sent Ġref erral Ġnot Ġfound Ġ[ id =$ id ]") ĊĊĠĠĠ Ġval Ġassigned By Ġ= Ġparse Auth User Token ( authentication ) ĊĠĠĠ Ġval Ġassigned To Ġ= ĠAuth User ( ĊĠĠĠĠĠ Ġid Ġ= Ġref erral Assignment . assigned To . userId , ĊĠĠĠĠĠ Ġauth Source Ġ= Ġref erral Assignment . assigned To . auth Source , ĊĠĠĠĠĠ ĠuserName Ġ= Ġref erral Assignment . assigned To . username , ĊĠĠĠ Ġ) ĊĠĠĠ Ġreturn ĠSent Ref erral DTO . from ( ĊĠĠĠĠĠ Ġref erral Service . assign Sent Ref erral ( sent Ref erral , Ġassigned By , Ġassigned To ) ĊĠĠĠ Ġ) ĊĠ Ġ} ĊĊĠ Ġ@ PostMapping ("/ draft - ref erral /{ id }/ send ") ĊĠ Ġfun Ġsend Draft Ref erral (@ PathVariable Ġid : ĠUUID , Ġauthentication : ĠJwt Authentication Token ): ĠResponseEntity < Sent Ref erral DTO > Ġ{ ĊĠĠĠ Ġval Ġdraft Ref erral Ġ= Ġref erral Service . get Draft Ref erral ( id ) ĊĠĠĠĠĠ Ġ?: Ġthrow ĠResponse Status Exception ( HttpStatus . NOT _ FOUND , Ġ" draft Ġref erral Ġnot Ġfound Ġ[ id =$ id ]") ĊĊĠĠĠ Ġval Ġuser Ġ= Ġparse Auth User Token ( authentication ) ĊĠĠĠ Ġval Ġsent Ref erral Ġ= Ġref erral Service . send Draft Ref erral ( draft Ref erral , Ġuser ) ĊĊĠĠĠ Ġval Ġlocation Ġ= ĠServlet Uri Components Builder ĊĠĠĠĠĠ Ġ. from Current Context Path () ĊĠĠĠĠĠ Ġ. path ("/ sent - ref erral /{ id }") ĊĠĠĠĠĠ Ġ. build And Expand ( sent Ref erral . id ) ĊĠĠĠĠĠ Ġ. to Uri () ĊĊĠĠĠ Ġreturn ĠResponseEntity ĊĠĠĠĠĠ Ġ. created ( location ) ĊĠĠĠĠĠ Ġ. body ( Sent Ref erral DTO . from ( sent Ref erral )) ĊĠ Ġ} ĊĊĠ Ġ@ GetMapping ("/ sent - ref erral /{ id }") ĊĠ Ġfun Ġget Sent Ref erral (@ PathVariable Ġid : ĠUUID ): ĠSent Ref erral DTO Ġ{ ĊĠĠĠ Ġreturn Ġref erral Service . get Sent Ref erral ( id ) ĊĠĠĠĠĠ Ġ? . let Ġ{ ĠSent Ref erral DTO . from ( it ) Ġ} ĊĠĠĠĠĠ Ġ?: Ġthrow ĠResponse Status Exception ( HttpStatus . NOT _ FOUND , Ġ" sent Ġref erral Ġnot Ġfound Ġ[ id =$ id ]") ĊĠ Ġ} ĊĊĠ Ġ@ GetMapping ("/ sent - ref err als ") ĊĠ Ġfun Ġget Sent Ref err als ( ĊĠĠĠ Ġ@ RequestParam Ġsent By : ĠString ? Ġ= Ġnull , ĊĠĠĠ Ġ@ RequestParam Ġsent To : ĠString ? Ġ= Ġnull , ĊĠĠĠ Ġ@ RequestParam Ġassigned To : ĠString ? Ġ= Ġnull , ĊĠ Ġ): ĠList < Sent Ref erral DTO > Ġ{ ĊĠĠĠ Ġif Ġ( listOf NotNull ( sent By , Ġsent To , Ġassigned To ). size Ġ!= Ġ 1 ) Ġ{ ĊĠĠĠĠĠ Ġthrow ĠServer Web Input Exception (" a Ġsingle Ġsearch Ġparameter Ġmust Ġbe Ġsupplied ") ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġsent By ?. let Ġ{ ĊĠĠĠĠĠ Ġreturn Ġref erral Service . get Sent Ref err als Sent By ( it ). map Ġ{ Ġref erral Ġ-> ĠSent Ref erral DTO . from ( ref erral ) Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġsent To ?. let Ġ{ ĊĠĠĠĠĠ Ġreturn Ġref erral Service . get Sent Ref err als For ServiceProvider ID ( it ). map Ġ{ Ġref erral Ġ-> ĠSent Ref erral DTO . from ( ref erral ) Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġassigned To ?. let Ġ{ ĊĠĠĠĠĠ Ġreturn Ġref erral Service . get Sent Ref err als Assigned To ( it ). map Ġ{ Ġref erral Ġ-> ĠSent Ref erral DTO . from ( ref erral ) Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġreturn Ġempty List () ĊĠ Ġ} ĊĊĠ Ġ@ PostMapping ("/ sent - ref erral /{ id }/ end ") ĊĠ Ġfun Ġend Sent Ref erral (@ PathVariable Ġid : ĠUUID , Ġauthentication : ĠJwt Authentication Token ): ĠEn ded Ref erral DTO Ġ{ ĊĠĠĠ Ġval Ġsent Ref erral Ġ= Ġref erral Service . get Sent Ref erral ( id ) ĊĠĠĠĠĠ Ġ?: Ġthrow ĠResponse Status Exception ( HttpStatus . NOT _ FOUND , Ġ" ref erral Ġnot Ġfound Ġ[ id =$ id ]") ĊĊĠĠĠ Ġval Ġuser Ġ= Ġjwt Auth User Mapper . map ( authentication ) ĊĠĠĠ Ġval Ġended Ref erral Ġ= Ġref erral Service . end Sent Ref erral ( sent Ref erral , Ġuser ) ĊĊĠĠĠ Ġreturn ĠEn ded Ref erral DTO . from ( ended Ref erral ) ĊĠ Ġ} ĊĊĠ Ġ@ PostMapping ("/ draft - ref erral ") ĊĠ Ġfun Ġcreate Draft Ref erral (@ RequestBody Ġcreate Ref erral Request DTO : ĠCreate Ref erral Request DTO , Ġauthentication : ĠJwt Authentication Token ): ĠResponseEntity < Draft Ref erral DTO > Ġ{ ĊĠĠĠ Ġval Ġuser Ġ= Ġparse Auth User Token ( authentication ) ĊĊĠĠĠ Ġval Ġref erral Ġ= Ġtry Ġ{ ĊĠĠĠĠĠ Ġref erral Service . create Draft Ref erral ( ĊĠĠĠĠĠĠĠ Ġuser , ĊĠĠĠĠĠĠĠ Ġcreate Ref erral Request DTO . service User Cr n , ĊĠĠĠĠĠĠĠ Ġcreate Ref erral Request DTO . inter vention Id , ĊĠĠĠĠĠ Ġ) ĊĠĠĠ Ġ} Ġcatch Ġ( e : ĠEntity NotFoundException ) Ġ{ ĊĠĠĠĠĠ Ġthrow ĠServer Web Input Exception (" invalid Ġinter vention Ġid Ġ[ id =${ create Ref erral Request DTO . inter vention Id }] ") ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġval Ġlocation Ġ= ĠServlet Uri Components Builder ĊĠĠĠĠĠ Ġ. from Current Request () ĊĠĠĠĠĠ Ġ. path ("/ { id }") ĊĠĠĠĠĠ Ġ. build And Expand ( ref erral . id ) ĊĠĠĠĠĠ Ġ. to Uri () ĊĊĠĠĠ Ġreturn ĠResponseEntity ĊĠĠĠĠĠ Ġ. created ( location ) ĊĠĠĠĠĠ Ġ. body ( Draft Ref erral DTO . from ( ref erral )) ĊĠ Ġ} ĊĊĠ Ġ@ GetMapping ("/ draft - ref erral /{ id }") ĊĠ Ġfun Ġget Draft Ref erral ByID (@ PathVariable Ġid : ĠUUID ): ĠDraft Ref erral DTO Ġ{ ĊĠĠĠ Ġreturn Ġref erral Service . get Draft Ref erral ( id ) ĊĠĠĠĠĠ Ġ? . let Ġ{ ĠDraft Ref erral DTO . from ( it ) Ġ} ĊĠĠĠĠĠ Ġ?: Ġthrow ĠResponse Status Exception ( HttpStatus . NOT _ FOUND , Ġ" draft Ġref erral Ġnot Ġfound Ġ[ id =$ id ]") ĊĠ Ġ} ĊĊĠ Ġ@ Patch Mapping ("/ draft - ref erral /{ id }") ĊĠ Ġfun Ġpatch Draft Ref erral ByID (@ PathVariable Ġid : ĠUUID , Ġ@ RequestBody Ġpartial Update : ĠDraft Ref erral DTO ): ĠDraft Ref erral DTO Ġ{ ĊĠĠĠ Ġval Ġref erral ToUpdate Ġ= Ġref erral Service . get Draft Ref erral ( id ) ĊĠĠĠĠĠ Ġ?: Ġthrow ĠResponse Status Exception ( HttpStatus . NOT _ FOUND , Ġ" draft Ġref erral Ġnot Ġfound Ġ[ id =$ id ]") ĊĊĠĠĠ Ġval Ġupdated Ref erral Ġ= Ġref erral Service . update Draft Ref erral ( ref erral ToUpdate , Ġpartial Update ) ĊĠĠĠ Ġreturn ĠDraft Ref erral DTO . from ( updated Ref erral ) ĊĠ Ġ} ĊĊĠ Ġ@ GetMapping ("/ draft - ref err als ") ĊĠ Ġfun Ġget Draft Ref err als CreatedBy UserID (@ RequestParam ĠuserID : ĠString ): ĠList < Draft Ref erral DTO > Ġ{ ĊĠĠĠ Ġreturn Ġref erral Service . get Draft Ref err als CreatedBy UserID ( userID ) ĊĠ Ġ} ĊĊĠ Ġ@ GetMapping ("/ service - category /{ id }") ĊĠ Ġfun Ġget Service Category ByID (@ PathVariable Ġid : ĠUUID ): ĠService Category DTO Ġ{ ĊĠĠĠ Ġreturn Ġservice Category Service . getService Category ByID ( id ) ĊĠĠĠĠĠ Ġ? . let Ġ{ ĠService Category DTO . from ( it ) Ġ} ĊĠĠĠĠĠ Ġ?: Ġthrow ĠResponse Status Exception ( HttpStatus . NOT _ FOUND , Ġ" service Ġcategory Ġnot Ġfound Ġ[ id =$ id ]") ĊĠ Ġ} ĊĊĠ Ġ@ GetMapping ("/ ref erral - c ancellation - re asons ") ĊĠ Ġfun Ġget Cancellation Re asons (): ĠC ancellation Re asons DTO Ġ{ ĊĠĠĠ Ġreturn ĠC ancellation Re asons DTO . from ( ref erral Service . getC ancellation Re asons ()) ĊĠ Ġ} ĊĊĠ Ġprivate Ġfun Ġparse Auth User Token ( authentication : ĠJwt Authentication Token ): ĠAuth User Ġ{ Ċ // ĠĠĠĠ Ġnote : Ġthis Ġdoes Ġnot Ġallow Ġtokens Ġfor Ġclient _ credentials Ġgrant Ġtypes Ġuse Ġthis ĠAPI ĊĠĠĠ Ġval ĠuserID Ġ= Ġauthentication . token . get Claim AsString (" user _ id ") ĊĠĠĠĠĠ Ġ?: Ġthrow ĠServer Web Input Exception (" no Ġ' user _ id ' Ġclaim Ġin Ġauthentication Ġtoken ") ĊĊĠĠĠ Ġval ĠuserName Ġ= Ġauthentication . token . get Claim AsString (" user _ name ") ĊĠĠĠĠĠ Ġ?: Ġthrow ĠServer Web Input Exception (" no Ġ' user _ name ' Ġclaim Ġin Ġauthentication Ġtoken ") ĊĊĠĠĠ Ġval Ġauth Source Ġ= Ġauthentication . token . get Claim AsString (" auth _ source ") ĊĠĠĠĠĠ Ġ?: Ġthrow ĠServer Web Input Exception (" no Ġ' auth _ source ' Ġclaim Ġin Ġauthentication Ġtoken ") ĊĊĠĠĠ Ġreturn ĠAuth User ( id Ġ= ĠuserID , Ġauth Source Ġ= Ġauth Source , ĠuserName Ġ= ĠuserName ) ĊĠ Ġ} Ċ } Ċ