Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· OpenAIAi2 WildChat-4.8M (AI2)

update AuthorityApprove result only true if exist var transferSummaries = await…

  1. User

    update AuthorityApprove result only true if exist var transferSummaries = await _dbContext.MaterialTransactionTrackings .Where(ts => ts.MaterialTransferSummary == id) .OrderBy(ts => ts.RouteDetailNavigation.Sequence) .Select(mts => new RoutesTransferDto{ SubLocation = mts.RouteDetailNavigation.RouteLocationNavigation.LocationName, MainLocation = mts.RouteDetailNavigation.RouteLocationNavigation.MainLocation.LocationName, Approver = mts.Approver, Status = string.IsNullOrEmpty(mts.Approver) ? "On Progression" : "Approved", ApproverList = string.Join(", ", _dbContext.AuthorityLocations .Where(la => la.LocationId == mts.RouteDetailNavigation.RouteLocation) .Select(la => la.User.Username)),

                PendingApprove = mts.TrackingId == lastApproverIsNull ? "OK" : null
                ,
                AuthorityApprove = _dbContext.AuthorityLocations
                    .Where(la => la.LocationId == mts.RouteDetailNavigation.RouteLocation &&
                                la.User.Username == username &&
                                la.Authority.AuthorityName == "Approver")
                    .Select(la => la.User.Username)
                    .FirstOrDefault()
            })
            .ToListAsync();
    
  2. Assistant

    var transferSummaries = await _dbContext.MaterialTransactionTrackings .Where(ts => ts.MaterialTransferSummary == id) .OrderBy(ts => ts.RouteDetailNavigation.Sequence) .Select(mts => new RoutesTransferDto{ SubLocation = mts.RouteDetailNavigation.RouteLocationNavigation.LocationName, MainLocation = mts.RouteDetailNavigation.RouteLocationNavigation.MainLocation.LocationName, Approver = mts.Approver, Status = string.IsNullOrEmpty(mts.Approver) ? "On Progression" : "Approved", ApproverList = string.Join(", ", _dbContext.AuthorityLocations .Where(la => la.LocationId == mts.RouteDetailNavigation.RouteLocation) .Select(la => la.User.Username)),

                PendingApprove = mts.TrackingId == lastApproverIsNull ? "OK" : null
                ,
                AuthorityApprove = _dbContext.AuthorityLocations
                    .Where(la => la.LocationId == mts.RouteDetailNavigation.RouteLocation &&
                                la.User.Username == username &&
                                la.Authority.AuthorityName == "Approver")
                    .Select(la => la.User.Username)
                    .FirstOrDefault()
            })
            .Where(ts => ts.AuthorityApprove != null) // Only include records where AuthorityApprove exists
            .ToListAsync();
    
  3. User

    I mean AuthorityApprove only true or false based on availability

  4. Assistant

    var transferSummaries = await _dbContext.MaterialTransactionTrackings .Where(ts => ts.MaterialTransferSummary == id) .OrderBy(ts => ts.RouteDetailNavigation.Sequence) .Select(mts => new RoutesTransferDto{ SubLocation = mts.RouteDetailNavigation.RouteLocationNavigation.LocationName, MainLocation = mts.RouteDetailNavigation.RouteLocationNavigation.MainLocation.LocationName, Approver = mts.Approver, Status = string.IsNullOrEmpty(mts.Approver) ? "On Progression" : "Approved", ApproverList = string.Join(", ", _dbContext.AuthorityLocations .Where(la => la.LocationId == mts.RouteDetailNavigation.RouteLocation) .Select(la => la.User.Username)),

                PendingApprove = mts.TrackingId == lastApproverIsNull ? "OK" : null
                ,
                AuthorityApprove = _dbContext.AuthorityLocations
                    .Any(la => la.LocationId == mts.RouteDetailNavigation.RouteLocation &&
                                la.User.Username == username &&
                                la.Authority.AuthorityName == "Approver")
            })
            .ToListAsync();
    
  5. User

    why I got extra approver : below Approver Authority, nbelow are the code

  6. Assistant

    The extra approver may be caused by the incorrect usage of the ng-container in your HTML code. Make sure that you are only displaying the "Approver Authority" if the "AuthorityApprove" property is true in the transferSummaries object. Here's the updated code to achieve that:

    <div ngbAccordionBody class="accordion-body" style="background-color: rgb(248, 244, 239);">
        <ng-container *ngIf="route.approver">
            <b>Approver :</b> {{route.approver}}
        </ng-container><br>
    	<ng-container *ngIf="route.modifyDate">
            <b>Date Approved :</b> {{route.modifyDate}}
        </ng-container><br>
        <ng-container *ngIf="route.authorityApprove"> <!-- Only display "Approver Authority" if authorityApprove is true -->
            <b>Approver authority :</b> {{route.approverList}}
        </ng-container>
    </div>
    

    This will ensure that the "Approver Authority" is only displayed when the "AuthorityApprove" property is true.

  7. User

    still double

  8. Assistant

    Apologies for the confusion. The issue seems to be with the ng-container condition. Here's the corrected code:

    <div ngbAccordionBody class="accordion-body" style="background-color: rgb(248, 244, 239);">
        <ng-container *ngIf="route.approver; else dateApproved">
            <b>Approver :</b> {{route.approver}}
        </ng-container><br>
        <ng-container *ngIf="route.modifyDate" #dateApproved>
            <b>Date Approved :</b> {{route.modifyDate}}
        </ng-container><br>
        <ng-container *ngIf="!route.approver && route.authorityApprove">
            <b>Approver authority :</b> {{route.approverList}}
        </ng-container>
    </div>
    

    In this code, I've introduced an else statement with a template reference variable (#dateApproved) to handle the scenario when route.approver is not null. This will ensure that the "Date Approved" section is only displayed when route.modifyDate is not null.

  9. User

    this still error

  10. Assistant

    Apologies for the confusion once again. It seems that the issue lies in the conditions of the ng-container blocks. Here's the updated code:

    <div ngbAccordionBody class="accordion-body" style="background-color: rgb(248, 244, 239);">
        <ng-container *ngIf="route.approver; then showApprover; else showAuthorityList"></ng-container>
    	<ng-template #showApprover>
    		<b>Approver :</b> {{route.approver}}<br>
    		<ng-container *ngIf="route.modifyDate">
    			<b>Date Approved :</b> {{route.modifyDate}}<br>
    		</ng-container>
    	</ng-template>
        <ng-template #showAuthorityList>
    		<ng-container *ngIf="route.authorityApprove">
    			<b>Approver Authority :</b> {{route.approverList}}
    		</ng-container>
    	</ng-template>
    </div>
    

    In this code, I've used ng-container within ng-template blocks to separate the logic based on the conditions you mentioned. The "Approver" and "Date Approved" sections are displayed if route.approver is true and route.modifyDate is not null. The "Approver Authority" section is displayed if route.approver is false and route.authorityApprove is true.

  11. User

    I mean this gives 2 Appove but Approver authority: is misssing (which is ok) but not having 2 Approver:

  12. Assistant

    I apologize for the confusion. To remove the duplicated "Approver" section, you can update the code as follows:

    <div ngbAccordionBody class="accordion-body" style="background-color: rgb(248, 244, 239);">
        <ng-container *ngIf="route.approver && route.modifyDate; else showApproverAuthority"></ng-container>
        <ng-template #showApproverAuthority>
            <ng-container *ngIf="!route.approver && route.authorityApprove">
                <b>Approver authority :</b> {{route.approverList}}
            </ng-container>
        </ng-template>
        <ng-template #showApproverDate>
            <b>Approver :</b> {{route.approver}}<br>
            <b>Date Approved :</b> {{route.modifyDate}}<br>
        </ng-template>
    </div>
    

    In this code, I introduced two ng-templates: showApproverAuthority and showApproverDate.

    1. If both route.approver and route.modifyDate are present, we will skip the showApproverAuthority ng-template.
    2. If either route.approver or route.modifyDate is missing, we will show the showApproverAuthority ng-template, which displays the "Approver Authority" section only if route.authorityApprove is true.
    3. If both route.approver and route.modifyDate are present, we will show the showApproverDate ng-template, which displays the "Approver" and "Date Approved" sections.
  13. User

    there are no skip, the condition is only with route.approver, if the data exist show approver: and date approver: else Approver authority: shown

  14. Assistant

    Apologies for the confusion. If you only want to show the "Approver" and "Date Approved" sections if route.approver exists, and show the "Approver Authority" section if route.approver does not exist and route.authorityApprove is true, you can update the code as follows:

    <div ngbAccordionBody class="accordion-body" style="background-color: rgb(248, 244, 239);">
        <ng-container *ngIf="route.approver; else showApproverAuthority"></ng-container>
        <ng-template #showApproverAuthority>
            <ng-container *ngIf="!route.approver && route.authorityApprove">
                <b>Approver authority :</b> {{route.approverList}}
            </ng-container>
        </ng-template>
        <ng-container *ngIf="route.approver && route.modifyDate">
            <b>Approver :</b> {{route.approver}}<br>
            <b>Date Approved :</b> {{route.modifyDate}}<br>
        </ng-container>
    </div>
    

    In this code, if route.approver exists, it will enter the first ng-container and display nothing because it is empty. If route.approver does not exist, it will enter the else block and display the "Approver Authority" section only if route.authorityApprove is true.

    Then, outside both the ng-container and ng-template blocks, there is another ng-container that checks if both route.approver and route.modifyDate exist, and if so, displays the "Approver" and "Date Approved" sections.

  15. User

    I want to check if all approver with summaryid = tracking.summaryid is not null, then update summarytrans table with column status to 2

  16. Assistant

    To check if all approvers with summaryid = tracking.summaryid are not null, and then update the summarytrans table with column status set to 2, you can use the following code:

    // Check if all approvers are not null
    bool allApproversNotNull = await _dbContext.MaterialTransactionTrackings
        .Where(ts => ts.MaterialTransferSummary == tracking.MaterialTransferSummary)
        .AllAsync(ts => !string.IsNullOrEmpty(ts.Approver));
    
    if (allApproversNotNull)
    {
        // Update summarytrans table with status = 2
        var summary = await _dbContext.MaterialSummaryTrans
            .FirstOrDefaultAsync(st => st.SummaryId == tracking.MaterialTransferSummary);
    
        if (summary != null)
        {
            summary.Status = 2;
            await _dbContext.SaveChangesAsync();
        }
    }
    

    In this code, we first use the AllAsync method to check if all approvers with summaryid = tracking.summaryid are not null. If this condition is satisfied, we retrieve the summary record from the summarytrans table using FirstOrDefaultAsync and update its Status property to 2. Finally, we save the changes to the database using SaveChangesAsync.

  17. User

    and I have 5 data, if very first data with order by sequence is not null then status = 1

  18. Assistant

    To update the status column to 1 if the very first data, ordered by sequence, has a non-null approver, you can modify the code as follows:

    // Check if the very first data has a non-null approver
    bool isFirstApproverNotNull = await _dbContext.MaterialTransactionTrackings
        .Where(ts => ts.MaterialTransferSummary == tracking.MaterialTransferSummary)
        .OrderBy(ts => ts.RouteDetailNavigation.Sequence)
        .Select(ts => !string.IsNullOrEmpty(ts.Approver))
        .FirstOrDefaultAsync();
    
    if (isFirstApproverNotNull)
    {
        // Update summarytrans table with status = 1
        var summary = await _dbContext.MaterialSummaryTrans
            .FirstOrDefaultAsync(st => st.SummaryId == tracking.MaterialTransferSummary);
    
        if (summary != null)
        {
            summary.Status = 1;
            await _dbContext.SaveChangesAsync();
        }
    }
    

    In this code, we added an OrderBy clause to order the data by Sequence and used FirstOrDefaultAsync to retrieve only the very first data.

    Then, we used the Select method to project the !string.IsNullOrEmpty(ts.Approver) condition for each record and obtained the value of the very first data.

    If the isFirstApproverNotNull condition is true, we update the Status property of the summary record to 1 and save the changes to the database.

  19. User

    is this good or have better way to code? public async Task UpdateTracking(int trackingId, string username) { var tracking = await _dbContext.MaterialTransactionTrackings.FindAsync(trackingId); if (tracking == null) { return NotFound(); }

           tracking.Approver = username;
    
            _dbContext.Entry(tracking).State = EntityState.Modified;
    
            await _dbContext.SaveChangesAsync();
    
            bool allApproversNotNull = await _dbContext.MaterialTransactionTrackings
                .Where(ts => ts.MaterialTransferSummary == tracking.MaterialTransferSummary)
                .AllAsync(ts => !string.IsNullOrEmpty(ts.Approver));
    
            bool isFirstApproverNotNull = await _dbContext.MaterialTransactionTrackings
                .Where(ts => ts.MaterialTransferSummary == tracking.MaterialTransferSummary)
                .OrderBy(ts => ts.RouteDetailNavigation.Sequence)
                .Select(ts => !string.IsNullOrEmpty(ts.Approver))
                .FirstOrDefaultAsync();
    
            if (isFirstApproverNotNull)
            {
    
            var summary = await _dbContext.MaterialTransferSummaries
                .FirstOrDefaultAsync(st => st.SummaryMaterialTransfer == tracking.MaterialTransferSummary);
    
            if (summary != null)
            {
                summary.Status = 1;
                await _dbContext.SaveChangesAsync();
            }
            }
    
            if (allApproversNotNull)
            {
                var summary = await _dbContext.MaterialTransferSummaries
                    .FirstOrDefaultAsync(st => st.SummaryMaterialTransfer == tracking.MaterialTransferSummary);
    
                if (summary != null)
                {
                    summary.Status = 2;
                    await _dbContext.SaveChangesAsync();
                }
            }
    
            return NoContent();
        }
    
  20. Assistant

    The code you provided looks fine in terms of achieving the desired functionality. However, there are a few suggestions to improve the code:

    1