11import { LOCALE_KEY } from '@/constants' ;
22import { db } from '@/database/db' ;
33import { channels , flows } from '@/database/schema' ;
4- import { TNewFlow , TUpdateFlow } from '@/database/types' ;
4+ import { TNewFlow } from '@/database/types' ;
5+ import { FlowDTO } from '@/dtos/flows.dto' ;
56import { PagingDTO } from '@/dtos/paging.dto' ;
67import { HttpException } from '@/exceptions/http-exception' ;
78import { LocaleService } from '@/i18n/ctx' ;
89import { FlowExtend } from '@/interfaces/flows.interface' ;
910import { Paging } from '@/interfaces/paging.interface' ;
1011import { and , asc , desc , eq , isNotNull , like , ne , notExists , sql } from 'drizzle-orm' ;
1112import { StatusCodes } from 'http-status-codes' ;
13+ import { omit } from 'lodash' ;
1214import { Inject , Service } from 'typedi' ;
1315import { ChannelService } from './channels.service' ;
1416
@@ -47,7 +49,12 @@ export class FlowService {
4749 return newFlow ;
4850 }
4951
50- public async updateFlowById ( id : string , fields : TUpdateFlow ) {
52+ public async updateFlowById ( { fields, id, userId } : {
53+ userId : string ;
54+ id : string ;
55+ fields : FlowDTO
56+ } ) {
57+
5158 const flowExisted = await db . query . flows . findFirst ( {
5259 where : eq ( flows . id , id ) ,
5360 } ) ;
@@ -63,7 +70,7 @@ export class FlowService {
6370 where : and (
6471 ne ( flows . id , id ) ,
6572 eq ( flows . name , fields . name ) ,
66- eq ( flows . userId , fields . userId ) ,
73+ eq ( flows . userId , userId ) ,
6774 eq ( flows . deleted , false )
6875 ) ,
6976 } ) ;
@@ -75,11 +82,27 @@ export class FlowService {
7582 ) ;
7683 }
7784
78- fields . updatedAt = new Date ( ) ;
85+ // handle update flowId for channel
86+ const addFlowIdForChannels = await this . chanelService . updateFlowId (
87+ fields . channelIds ,
88+ flowExisted . id
89+ ) ;
90+
91+ if ( ! addFlowIdForChannels ) {
92+ throw new HttpException (
93+ StatusCodes . BAD_REQUEST ,
94+ this . localeService . i18n ( ) . FLOW . ADD_MULTIPLE_CHANNELS_FLOW__FAILED ( )
95+ ) ;
96+ }
97+
7998
8099 const [ updateFlow ] = await db
81100 . update ( flows )
82- . set ( fields )
101+ . set ( {
102+ ...omit ( fields , [ 'channelIds' ] ) ,
103+ updatedAt : new Date ( )
104+
105+ } )
83106 . where ( eq ( flows . id , id ) )
84107 . returning ( ) ;
85108
0 commit comments