-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCallCycleController.cs
More file actions
184 lines (154 loc) · 5.53 KB
/
Copy pathCallCycleController.cs
File metadata and controls
184 lines (154 loc) · 5.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace SalesAPI.Controllers;
[ApiController]
[Route("api/[controller]")]
[Authorize]
public class CallCycleController : ControllerBase
{
private readonly DatabaseHelper _db;
public CallCycleController(DatabaseHelper db)
{
_db = db;
}
/// <summary>
/// Get all call cycle entries
/// </summary>
[HttpGet]
public IActionResult GetAll()
{
var cycles = _db.GetAllCallCycles();
return Ok(cycles);
}
/// <summary>
/// Upload call cycle Excel file (Admin only)
/// </summary>
[Authorize(Roles = "Admin")]
[HttpPost("upload")]
public async Task<IActionResult> UploadCallCycle(IFormFile file)
{
if (file == null || file.Length == 0)
return BadRequest(new { message = "No file uploaded" });
var tempPath = Path.GetTempFileName();
try
{
using (var stream = new FileStream(tempPath, FileMode.Create))
{
await file.CopyToAsync(stream);
}
var cycles = ExcelParser.ParseCallCycle(tempPath);
// Clear existing call cycle
_db.ClearCallCycle();
// Insert new entries
foreach (var cycle in cycles)
{
// Try to match client by name
var matchedClient = _db.GetAllClients().FirstOrDefault(c =>
c.CompanyName.Equals(cycle.ClientName, StringComparison.OrdinalIgnoreCase));
if (matchedClient != null)
{
cycle.ClientId = matchedClient.Id;
}
_db.UpsertCallCycle(cycle);
}
return Ok(new
{
message = "Call cycle uploaded successfully",
total = cycles.Count
});
}
finally
{
if (System.IO.File.Exists(tempPath))
System.IO.File.Delete(tempPath);
}
}
/// <summary>
/// Get calendar view with actual dates calculated from start date
/// Starts Monday, January 26, 2026 as Week 1 Day 1
/// </summary>
[HttpGet("calendar")]
public IActionResult GetCalendar([FromQuery] string? startDate = null)
{
// Default start date: Monday, January 26, 2026
DateTime cycleStart = new DateTime(2026, 1, 26);
if (!string.IsNullOrEmpty(startDate) && DateTime.TryParse(startDate, out DateTime customStart))
{
cycleStart = customStart;
}
var cycles = _db.GetAllCallCycles();
var appointments = _db.GetAppointmentRequests();
var calendar = new List<CallCycleDay>();
string[] days = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" };
for (int week = 1; week <= 4; week++)
{
for (int day = 0; day < 5; day++)
{
int daysOffset = ((week - 1) * 7) + day;
DateTime actualDate = cycleStart.AddDays(daysOffset);
var dayData = new CallCycleDay
{
WeekNumber = week,
DayOfWeek = days[day],
ActualDate = actualDate,
Clients = cycles.Where(c =>
c.WeekNumber == week &&
c.DayOfWeek.Equals(days[day], StringComparison.OrdinalIgnoreCase))
.OrderBy(c => c.OrderInDay)
.ToList(),
Appointments = appointments.Where(a =>
a.RequestedDate == actualDate.ToString("yyyy-MM-dd") &&
a.Status == "Approved")
.ToList()
};
calendar.Add(dayData);
}
}
return Ok(calendar);
}
/// <summary>
/// Get current week number based on start date
/// </summary>
[HttpGet("current-week")]
public IActionResult GetCurrentWeek()
{
DateTime cycleStart = new DateTime(2026, 1, 26);
DateTime today = DateTime.Today;
int daysSinceStart = (today - cycleStart).Days;
if (daysSinceStart < 0)
{
return Ok(new { weekNumber = 0, message = "Cycle hasn't started yet" });
}
// 4-week cycle that repeats
int weekNumber = ((daysSinceStart / 7) % 4) + 1;
return Ok(new {
weekNumber,
cycleDay = daysSinceStart % 28,
startDate = cycleStart.ToString("yyyy-MM-dd")
});
}
/// <summary>
/// Get appointment requests (pending by default)
/// </summary>
[HttpGet("appointments")]
public IActionResult GetAppointments([FromQuery] string? status = "Pending")
{
var appointments = _db.GetAppointmentRequests(status);
return Ok(appointments);
}
/// <summary>
/// Update appointment request status (Admin only)
/// </summary>
[Authorize(Roles = "Admin")]
[HttpPut("appointments/{id}")]
public IActionResult UpdateAppointment(int id, [FromBody] UpdateAppointmentRequest request)
{
_db.UpdateAppointmentStatus(id, request.Status, request.ResponseNotes);
return Ok(new { message = "Appointment updated successfully" });
}
}
public class UpdateAppointmentRequest
{
public string Status { get; set; } = string.Empty;
public string? ResponseNotes { get; set; }
}