diff --git a/src/Api/TakeoutSaaS.TenantApi/Controllers/StoreStaffController.cs b/src/Api/TakeoutSaaS.TenantApi/Controllers/StoreStaffController.cs index d4df9c8..28a8d2f 100644 --- a/src/Api/TakeoutSaaS.TenantApi/Controllers/StoreStaffController.cs +++ b/src/Api/TakeoutSaaS.TenantApi/Controllers/StoreStaffController.cs @@ -313,12 +313,8 @@ public sealed class StoreStaffController( ? CreateOffWeekShifts() : NormalizeShifts(request.Shifts, fallback, template); - dbContext.StoreStaffWeeklySchedules.RemoveRange(existingRows); - await dbContext.StoreStaffWeeklySchedules.AddRangeAsync( - ToWeeklyEntities(parsedStoreId, parsedStaffId, shifts, template), - cancellationToken); - - await dbContext.SaveChangesAsync(cancellationToken); + var nextRows = ToWeeklyEntities(parsedStoreId, parsedStaffId, shifts, template).ToList(); + await ReplaceWeeklySchedulesAsync(existingRows, nextRows, cancellationToken); return ApiResponse.Ok(new StaffScheduleDto { @@ -404,12 +400,10 @@ public sealed class StoreStaffController( }); } - dbContext.StoreStaffWeeklySchedules.RemoveRange(existingRows); var entities = finalSchedules .SelectMany(x => ToWeeklyEntities(parsedStoreId, long.Parse(x.StaffId), x.Shifts, template)) .ToList(); - await dbContext.StoreStaffWeeklySchedules.AddRangeAsync(entities, cancellationToken); - await dbContext.SaveChangesAsync(cancellationToken); + await ReplaceWeeklySchedulesAsync(existingRows, entities, cancellationToken); return ApiResponse.Ok(new StoreStaffScheduleDto { @@ -513,11 +507,11 @@ public sealed class StoreStaffController( targetTemplateEntity.FullStartTime = StoreApiHelpers.ParseRequiredTime(sourceTemplate.Full.StartTime, "templates.full.startTime"); targetTemplateEntity.FullEndTime = StoreApiHelpers.ParseRequiredTime(sourceTemplate.Full.EndTime, "templates.full.endTime"); } + await dbContext.SaveChangesAsync(cancellationToken); var targetRows = await dbContext.StoreStaffWeeklySchedules .Where(x => x.TenantId == tenantId && accessibleTargetIds.Contains(x.StoreId)) .ToListAsync(cancellationToken); - dbContext.StoreStaffWeeklySchedules.RemoveRange(targetRows); var targetStaffs = await dbContext.MerchantStaff .AsNoTracking() @@ -547,12 +541,7 @@ public sealed class StoreStaffController( } } - if (entities.Count > 0) - { - await dbContext.StoreStaffWeeklySchedules.AddRangeAsync(entities, cancellationToken); - } - - await dbContext.SaveChangesAsync(cancellationToken); + await ReplaceWeeklySchedulesAsync(targetRows, entities, cancellationToken); return ApiResponse.Ok(new CopyStoreStaffScheduleResult { @@ -774,7 +763,11 @@ public sealed class StoreStaffController( IEnumerable shifts, StoreShiftTemplatesDto template) { - foreach (var shift in shifts) + foreach (var shift in shifts + .Where(x => x.DayOfWeek is >= 0 and <= 6) + .GroupBy(x => x.DayOfWeek) + .Select(x => x.Last()) + .OrderBy(x => x.DayOfWeek)) { var shiftType = StoreApiHelpers.ToShiftType(shift.ShiftType); var (defaultStart, defaultEnd) = ResolveShiftTimeRange(shiftType, template); @@ -801,6 +794,26 @@ public sealed class StoreStaffController( } } + private async Task ReplaceWeeklySchedulesAsync( + IReadOnlyCollection existingRows, + IReadOnlyCollection nextRows, + CancellationToken cancellationToken) + { + if (existingRows.Count > 0) + { + dbContext.StoreStaffWeeklySchedules.RemoveRange(existingRows); + await dbContext.SaveChangesAsync(cancellationToken); + } + + if (nextRows.Count == 0) + { + return; + } + + await dbContext.StoreStaffWeeklySchedules.AddRangeAsync(nextRows, cancellationToken); + await dbContext.SaveChangesAsync(cancellationToken); + } + private static List CreateOffWeekShifts() { return Enumerable.Range(0, 7).Select(day => new StaffDayShiftDto