@@ -447,6 +447,8 @@ export class WeeklySummariesReport extends Component {
447447 ) ;
448448 } ) ;
449449
450+ this . setState ( { filteredSummaries : temp } ) ;
451+
450452 if ( selectedCodes [ 0 ] ?. value === '' || selectedCodes . length >= 52 ) {
451453 if ( selectedCodes . length >= 52 ) {
452454 selectedCodes . forEach ( code => {
@@ -668,66 +670,74 @@ export class WeeklySummariesReport extends Component {
668670
669671 handleAllTeamCodeReplace = async ( ) => {
670672 try {
671- const { replaceCode } = this . state ;
673+ const { replaceCode, selectedCodes , summaries , teamCodes } = this . state ;
672674 this . setState ( { replaceCodeLoading : true } ) ;
673- const boolean = fullCodeRegex . test ( replaceCode ) ;
674- if ( boolean ) {
675- const userIds = this . state . selectedCodes . flatMap ( item => item . _ids ) ;
676- const url = ENDPOINTS . USERS_ALLTEAMCODE_CHANGE ;
677- const payload = {
678- userIds,
679- replaceCode,
680- } ;
681- try {
682- const data = await axios . patch ( url , payload ) ;
683- const userObjs = userIds . reduce ( ( acc , curr ) => {
684- acc [ curr ] = true ;
685- return acc ;
686- } , { } ) ;
687- if ( data ?. data ?. isUpdated ) {
688- this . handleTeamCodeChange ( '' , replaceCode , userObjs ) ;
689-
690- await this . props . getAllTeamCode ( ) ;
691-
692- const updatedSummaries = [ ...this . state . summaries ] ;
693- const teamCodeGroup = { } ;
694-
695- updatedSummaries . forEach ( summary => {
696- const code = summary . teamCode || 'noCodeLabel' ;
697- if ( teamCodeGroup [ code ] ) {
698- teamCodeGroup [ code ] . push ( summary ) ;
699- } else {
700- teamCodeGroup [ code ] = [ summary ] ;
701- }
702- } ) ;
703675
704- const updatedTeamCodes = Object . keys ( teamCodeGroup ) . map ( code => ( {
705- value : code ,
706- label : `${ code } (${ teamCodeGroup [ code ] . length } )` ,
707- _ids : teamCodeGroup [ code ] ?. map ( item => item . _id ) ,
708- } ) ) ;
676+ const isValidCode = fullCodeRegex . test ( replaceCode ) ;
677+ if ( ! isValidCode ) {
678+ this . setState ( {
679+ replaceCodeError : 'NOT SAVED! The code must be between 5 and 7 characters long.' ,
680+ } ) ;
681+ return ;
682+ }
709683
710- this . props . setTeamCodes ( updatedTeamCodes ) ;
711- this . setState ( { replaceCode : '' , replaceCodeError : null } ) ;
712- this . filterWeeklySummaries ( ) ;
713- } else {
714- this . setState ( {
715- replaceCode : '' ,
716- replaceCodeError : 'Update failed Please try again with another code!' ,
717- } ) ;
684+ const oldTeamCodes = selectedCodes . map ( code => code . value ) ;
685+
686+ // Call the new backend API with a POST request
687+ const response = await axios . post ( ENDPOINTS . REPLACE_TEAM_CODE , {
688+ oldTeamCodes,
689+ newTeamCode : replaceCode ,
690+ } ) ;
691+
692+ if ( response . data ?. updatedCount > 0 ) {
693+ // Update the summaries in the local state
694+ const updatedSummaries = summaries . map ( summary => {
695+ if ( oldTeamCodes . includes ( summary . teamCode ) ) {
696+ return { ...summary , teamCode : replaceCode } ;
718697 }
719- } catch ( err ) {
720- this . setState ( { replaceCode : '' , replaceCodeError : err . toJSON ( ) . message } ) ;
721- }
698+ return summary ;
699+ } ) ;
700+
701+ // Remove old team codes and add the new team code in teamCodes
702+ const updatedTeamCodes = teamCodes
703+ . filter ( teamCode => ! oldTeamCodes . includes ( teamCode . value ) ) // Remove old team codes
704+ . concat ( {
705+ value : replaceCode ,
706+ label : `${ replaceCode } (${
707+ updatedSummaries . filter ( s => s . teamCode === replaceCode ) . length
708+ } )`,
709+ _ids : updatedSummaries . filter ( s => s . teamCode === replaceCode ) . map ( s => s . _id ) ,
710+ } ) ;
711+
712+ // Remove old team codes and add the new team code in selectedCodes
713+ const updatedSelectedCodes = selectedCodes
714+ . filter ( code => ! oldTeamCodes . includes ( code . value ) ) // Remove old team codes
715+ . concat ( {
716+ value : replaceCode ,
717+ label : `${ replaceCode } (${
718+ updatedSummaries . filter ( s => s . teamCode === replaceCode ) . length
719+ } )`,
720+ _ids : updatedSummaries . filter ( s => s . teamCode === replaceCode ) . map ( s => s . _id ) ,
721+ } ) ;
722+
723+ this . setState ( {
724+ summaries : updatedSummaries ,
725+ teamCodes : updatedTeamCodes ,
726+ selectedCodes : updatedSelectedCodes ,
727+ replaceCode : '' ,
728+ replaceCodeError : null ,
729+ } ) ;
730+
731+ // Re-filter the summaries to update the table
732+ this . filterWeeklySummaries ( ) ;
722733 } else {
723734 this . setState ( {
724- replaceCodeError : 'NOT SAVED! The code must be between 5 and 7 characters long .' ,
735+ replaceCodeError : 'No users found with the selected team codes .' ,
725736 } ) ;
726737 }
727738 } catch ( error ) {
728739 this . setState ( {
729- replaceCode : '' ,
730- replaceCodeError : 'Something went wrong please try again!' ,
740+ replaceCodeError : 'Something went wrong. Please try again!' ,
731741 } ) ;
732742 } finally {
733743 this . setState ( { replaceCodeLoading : false } ) ;
0 commit comments