11import { Request , Response } from 'express' ;
22import Joi from 'joi' ;
3+ import { ACCOUNT_FCM_DEVICE_PLATFORM_VALUES , AccountFCMDevicePlatformValues } from 'podverse-helpers' ;
34import { AccountFCMDeviceService } from 'podverse-orm' ;
45import { handleGenericErrorResponse } from '@api/controllers/helpers/error' ;
56import { validateBodyObject } from '@api/lib/validation' ;
67import { ensureAuthenticated } from '@api/lib/auth' ;
78
89const createAccountFCMDeviceSchema = Joi . object ( {
9- fcm_token : Joi . string ( ) . required ( )
10+ fcm_token : Joi . string ( ) . required ( ) ,
11+ installation_id : Joi . string ( ) . required ( ) ,
12+ platform : Joi . string ( ) . valid ( ...ACCOUNT_FCM_DEVICE_PLATFORM_VALUES ) . required ( )
1013} ) ;
1114
1215const updateAccountFCMDeviceSchema = Joi . object ( {
13- previous_fcm_token : Joi . string ( ) . required ( ) ,
14- new_fcm_token : Joi . string ( ) . required ( )
16+ new_fcm_token : Joi . string ( ) . required ( ) ,
17+ installation_id : Joi . string ( ) . required ( ) ,
18+ previous_fcm_token : Joi . string ( ) . required ( ) . allow ( null ) ,
19+ platform : Joi . string ( ) . valid ( ...ACCOUNT_FCM_DEVICE_PLATFORM_VALUES ) . required ( )
1520} ) ;
1621
1722const deleteAccountFCMDeviceSchema = Joi . object ( {
18- account_id : Joi . string ( ) . required ( ) ,
19- fcm_token : Joi . string ( ) . required ( )
23+ fcm_token : Joi . string ( ) . required ( ) . allow ( null ) ,
24+ installation_id : Joi . string ( ) . required ( ) . allow ( null ) ,
2025} ) ;
2126
22- class AccountFCMDeviceController {
27+ const updateLocaleForAccountSchema = Joi . object ( {
28+ locale : Joi . string ( ) . required ( )
29+ } ) ;
30+
31+ export class AccountFCMDeviceController {
2332 private static accountFCMDeviceService = new AccountFCMDeviceService ( ) ;
2433
2534 static async create ( req : Request , res : Response ) : Promise < void > {
2635 ensureAuthenticated ( req , res , async ( ) => {
2736 validateBodyObject ( createAccountFCMDeviceSchema , req , res , async ( ) => {
2837 try {
2938 const jwtUser = req . user ! ;
30- const { fcm_token } = req . body ;
31- const accountFCMDevice = await AccountFCMDeviceController . accountFCMDeviceService . create ( jwtUser . id , fcm_token ) ;
39+ const { fcm_token, installation_id, platform } = req . body as {
40+ fcm_token : string ;
41+ installation_id : string ;
42+ platform : AccountFCMDevicePlatformValues ;
43+ } ;
44+ const accountFCMDevice = await AccountFCMDeviceController
45+ . accountFCMDeviceService . create ( jwtUser . id , {
46+ fcm_token,
47+ installation_id,
48+ platform
49+ } ) ;
3250 res . json ( accountFCMDevice ) ;
3351 } catch ( error ) {
3452 handleGenericErrorResponse ( res , error ) ;
@@ -42,8 +60,19 @@ class AccountFCMDeviceController {
4260 validateBodyObject ( updateAccountFCMDeviceSchema , req , res , async ( ) => {
4361 try {
4462 const jwtUser = req . user ! ;
45- const { previous_fcm_token, new_fcm_token } = req . body ;
46- const accountFCMDevice = await AccountFCMDeviceController . accountFCMDeviceService . update ( jwtUser . id , previous_fcm_token , new_fcm_token ) ;
63+ const { previous_fcm_token, new_fcm_token, installation_id, platform } = req . body as {
64+ new_fcm_token : string ;
65+ installation_id : string ;
66+ previous_fcm_token : string | null ;
67+ platform : AccountFCMDevicePlatformValues ;
68+ } ;
69+ const accountFCMDevice = await AccountFCMDeviceController
70+ . accountFCMDeviceService . update ( jwtUser . id , {
71+ new_fcm_token,
72+ installation_id,
73+ previous_fcm_token,
74+ platform
75+ } ) ;
4776 res . json ( accountFCMDevice ) ;
4877 } catch ( error ) {
4978 handleGenericErrorResponse ( res , error ) ;
@@ -57,15 +86,47 @@ class AccountFCMDeviceController {
5786 validateBodyObject ( deleteAccountFCMDeviceSchema , req , res , async ( ) => {
5887 try {
5988 const jwtUser = req . user ! ;
60- const { fcm_token } = req . body ;
61- await AccountFCMDeviceController . accountFCMDeviceService . delete ( jwtUser . id , fcm_token ) ;
89+ const { fcm_token, installation_id } = req . body as {
90+ fcm_token : string | null ;
91+ installation_id : string | null ;
92+ } ;
93+ await AccountFCMDeviceController
94+ . accountFCMDeviceService . delete ( jwtUser . id , {
95+ fcm_token : fcm_token ?? null ,
96+ installation_id : installation_id ?? null
97+ } ) ;
6298 res . json ( { message : 'FCM device deleted successfully' } ) ;
6399 } catch ( error ) {
64100 handleGenericErrorResponse ( res , error ) ;
65101 }
66102 } ) ;
67103 } ) ;
68104 }
69- }
70105
71- export { AccountFCMDeviceController } ;
106+ static async getAllForAccount ( req : Request , res : Response ) : Promise < void > {
107+ ensureAuthenticated ( req , res , async ( ) => {
108+ try {
109+ const jwtUser = req . user ! ;
110+ const devices = await AccountFCMDeviceController . accountFCMDeviceService . getAllForAccount ( jwtUser . id ) ;
111+ res . json ( devices ) ;
112+ } catch ( error ) {
113+ handleGenericErrorResponse ( res , error ) ;
114+ }
115+ } ) ;
116+ }
117+
118+ static async updateLocaleForAccount ( req : Request , res : Response ) : Promise < void > {
119+ ensureAuthenticated ( req , res , async ( ) => {
120+ validateBodyObject ( updateLocaleForAccountSchema , req , res , async ( ) => {
121+ try {
122+ const jwtUser = req . user ! ;
123+ const { locale } = req . body as { locale : string } ;
124+ await AccountFCMDeviceController . accountFCMDeviceService . updateLocaleForAccount ( jwtUser . id , { locale } ) ;
125+ res . json ( { message : 'Locale updated for account devices' } ) ;
126+ } catch ( error ) {
127+ handleGenericErrorResponse ( res , error ) ;
128+ }
129+ } ) ;
130+ } ) ;
131+ }
132+ }
0 commit comments