Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 2 additions & 16 deletions Core/Resgrid.Model/PushUri.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ namespace Resgrid.Model
[Table("PushUris")]
public class PushUri : IEntity
{
private string _pushLocation;

[Key]
[Required]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
Expand All @@ -36,20 +34,8 @@ public class PushUri : IEntity
public string DeviceId { get; set; }

[Required]
public string PushLocation
{
get { return _pushLocation; }
set
{
if (_pushLocation != value)
{
_pushLocation = value;

//if (((Platforms)PlatformType) == Platforms.Windows8 || ((Platforms)PlatformType) == Platforms.WindowsPhone7 || ((Platforms)PlatformType) == Platforms.WindowsPhone8 || ((Platforms)PlatformType) == Platforms.UnitWin)
// ChannelUri = new Uri(_pushLocation, UriKind.Absolute);
}
}
}
[ProtoMember(9)]
public string PushLocation { get; set; }

[ProtoMember(5)]
public int? UnitId { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion Core/Resgrid.Services/PushService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public PushService(IPushLogsService pushLogsService, INotificationProvider notif

public async Task<bool> Register(PushUri pushUri)
{
if (pushUri == null || String.IsNullOrWhiteSpace(pushUri.DeviceId))
if (pushUri == null || String.IsNullOrWhiteSpace(pushUri.DeviceId) || string.IsNullOrWhiteSpace(pushUri.PushLocation))
return false;
Comment on lines +38 to 39
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Good guard; align style and mirror DeviceId check in RegisterUnit.

  • Minor: use string.IsNullOrWhiteSpace consistently.
  • Important: RegisterUnit should also validate DeviceId.
-			if (pushUri == null || String.IsNullOrWhiteSpace(pushUri.DeviceId) || string.IsNullOrWhiteSpace(pushUri.PushLocation))
+			if (pushUri == null || string.IsNullOrWhiteSpace(pushUri.DeviceId) || string.IsNullOrWhiteSpace(pushUri.PushLocation))
 				return false;

Also update RegisterUnit’s guard (outside this hunk) to include DeviceId:

if (pushUri == null || !pushUri.UnitId.HasValue || string.IsNullOrWhiteSpace(pushUri.DeviceId) || string.IsNullOrWhiteSpace(pushUri.PushLocation))
	return false;
🤖 Prompt for AI Agents
In Core/Resgrid.Services/PushService.cs around lines 38-39, change the guard to
use the lowercase string.IsNullOrWhiteSpace for consistency and ensure
RegisterUnit’s guard (elsewhere in the file) mirrors the same checks by adding a
DeviceId validation; update RegisterUnit’s null/unit check to also verify
DeviceId is not null/whitespace in addition to UnitId and PushLocation so both
entrypoints validate DeviceId consistently.


var code = pushUri.PushLocation;
Expand Down
9 changes: 7 additions & 2 deletions Web/Resgrid.Web.Services/Controllers/v4/DevicesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,13 @@ public async Task<ActionResult<PushRegistrationResult>> RegisterDevice([FromBody
push.UserId = UserId;
push.PlatformType = registrationInput.Platform;

var department = await _departmentsService.GetDepartmentByIdAsync(DepartmentId, false);
push.PushLocation = department.Code;
if (!string.IsNullOrWhiteSpace(registrationInput.Prefix))
push.PushLocation = registrationInput.Prefix;
else
{
var department = await _departmentsService.GetDepartmentByIdAsync(DepartmentId, false);
push.PushLocation = department.Code;
}
Comment on lines +157 to +163
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Avoid NRE and guarantee non-empty PushLocation; trim Prefix and validate department fallback.

If department is null (or Code empty) this throws, and PushService.Register rejects empty locations. Harden this block.

-					if (!string.IsNullOrWhiteSpace(registrationInput.Prefix))
-						push.PushLocation = registrationInput.Prefix;
-					else
-					{
-						var department = await _departmentsService.GetDepartmentByIdAsync(DepartmentId, false);
-						push.PushLocation = department.Code;
-					}
+					if (!string.IsNullOrWhiteSpace(registrationInput.Prefix?.Trim()))
+					{
+						push.PushLocation = registrationInput.Prefix.Trim();
+					}
+					else
+					{
+						var department = await _departmentsService.GetDepartmentByIdAsync(DepartmentId, false);
+						if (department == null || string.IsNullOrWhiteSpace(department.Code))
+							return BadRequest();
+						push.PushLocation = department.Code;
+					}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if (!string.IsNullOrWhiteSpace(registrationInput.Prefix))
push.PushLocation = registrationInput.Prefix;
else
{
var department = await _departmentsService.GetDepartmentByIdAsync(DepartmentId, false);
push.PushLocation = department.Code;
}
if (!string.IsNullOrWhiteSpace(registrationInput.Prefix?.Trim()))
{
push.PushLocation = registrationInput.Prefix.Trim();
}
else
{
var department = await _departmentsService.GetDepartmentByIdAsync(DepartmentId, false);
if (department == null || string.IsNullOrWhiteSpace(department.Code))
return BadRequest();
push.PushLocation = department.Code;
}
🤖 Prompt for AI Agents
In Web/Resgrid.Web.Services/Controllers/v4/DevicesController.cs around lines 157
to 163, trim registrationInput.Prefix and only use it if not null/whitespace;
otherwise await _departmentsService.GetDepartmentByIdAsync and verify the
department is not null and department.Code is not null/whitespace (use trimmed
values). If neither provides a valid non-empty PushLocation, return a BadRequest
(or appropriate error) instead of assigning an empty value so
PushService.Register won’t be given an empty location.


push.DeviceId = registrationInput.Token;
push.Uuid = registrationInput.DeviceUuid;
Expand Down
Loading