forked from grandnode/grandnode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPushNotificationsController.cs
More file actions
83 lines (77 loc) · 3.09 KB
/
Copy pathPushNotificationsController.cs
File metadata and controls
83 lines (77 loc) · 3.09 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
using Grand.Core;
using Grand.Core.Domain.PushNotifications;
using Grand.Framework.Mvc;
using Grand.Services.Localization;
using Grand.Services.Logging;
using Grand.Services.PushNotifications;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Threading.Tasks;
namespace Grand.Web.Controllers
{
public class PushNotificationsController : BasePublicController
{
private readonly IWorkContext _workContext;
private readonly IPushNotificationsService _pushNotificationsService;
private readonly ILogger _logger;
private readonly ILocalizationService _localizationService;
public PushNotificationsController(IWorkContext workContext, IPushNotificationsService pushNotificationsService, ILogger logger,
ILocalizationService localizationService)
{
this._workContext = workContext;
this._pushNotificationsService = pushNotificationsService;
this._logger = logger;
this._localizationService = localizationService;
}
[HttpPost]
public virtual async Task<IActionResult> ProcessRegistration(bool success, string value)
{
if (success)
{
var toUpdate = await _pushNotificationsService.GetPushReceiverByCustomerId(_workContext.CurrentCustomer.Id);
if (toUpdate == null)
{
await _pushNotificationsService.InsertPushReceiver(new PushRegistration
{
CustomerId = _workContext.CurrentCustomer.Id,
Token = value,
RegisteredOn = DateTime.UtcNow,
Allowed = true
});
}
else
{
toUpdate.Token = value;
toUpdate.RegisteredOn = DateTime.UtcNow;
toUpdate.Allowed = true;
await _pushNotificationsService.UpdatePushReceiver(toUpdate);
}
}
else
{
if (value == "Permission denied")
{
var toUpdate = await _pushNotificationsService.GetPushReceiverByCustomerId(_workContext.CurrentCustomer.Id);
if (toUpdate == null)
{
await _pushNotificationsService.InsertPushReceiver(new PushRegistration
{
CustomerId = _workContext.CurrentCustomer.Id,
Token = "[DENIED]",
RegisteredOn = DateTime.UtcNow,
Allowed = false
});
}
else
{
toUpdate.Token = "[DENIED]";
toUpdate.RegisteredOn = DateTime.UtcNow;
toUpdate.Allowed = false;
await _pushNotificationsService.UpdatePushReceiver(toUpdate);
}
}
}
return new NullJsonResult();
}
}
}