Notifications
Comment suivre ce chapitre
Entrées : Identifiants SUPERADMIN existants et URL de callback contrôlée par vous.
Étapes et résultat attendu : Créez ou identifiez le destinataire administratif, puis envoyez la sélection complète de notifications. adminUserId désigne ici le destinataire, séparément du userId du parcours client.
Alternatives et actions ultérieures : Intégration administrative facultative. Subscribe remplace la sélection ; incluez tous les événements que vous voulez conserver.
Code source des DTO : AdminOutputModel, SystemOccurrenceOutputModel, SystemOccurrencePreferenceInputModel
Les variables affectées dans un bloc précédent sont réutilisées dans les suivants. Les saisies du client proviennent de votre interface. Conservez les identifiants et les valeurs de référence dans le contexte du client ; ne recréez pas ce contexte à chaque étape.
Les notifications peuvent être envoyées par :
- SMS
- Callback HTTP
Abonnement
var createAdminPayload = new
{
Name = name,
Surname = surname,
Email = email,
PhoneNumberPrefix = phoneNumberPrefix,
PhoneNumberNumber = phoneNumberNumber,
Url = callbackUrl,
GenderId = genderId,
LanguageId = languageId
};
// Creating an ADMIN user requires an existing SUPERADMIN token.
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", superAdminToken);
using var response = await httpClient.PostAsync("api/v1/users/admins",
new StringContent(JsonConvert.SerializeObject(createAdminPayload), Encoding.UTF8, "application/json"));
response.EnsureSuccessStatusCode();
var admin = JsonConvert.DeserializeObject<AdminOutputModel>(await response.Content.ReadAsStringAsync());
var adminUserId = admin.Id;
JsonObject payload = new JsonObject();
payload.addProperty("Name", name);
payload.addProperty("Surname", surname);
payload.addProperty("Email", email);
payload.addProperty("PhoneNumberPrefix", phoneNumberPrefix);
payload.addProperty("PhoneNumberNumber", phoneNumberNumber);
payload.addProperty("Url", callbackUrl);
payload.addProperty("GenderId", genderId);
payload.addProperty("LanguageId", languageId);
HttpRequest request = HttpRequest.newBuilder()
.uri(Settings.wmsUri("users/admins"))
.timeout(Duration.ofMinutes(1))
.header("Authorization", "Bearer " + Settings.SUPERADMIN_TOKEN)
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(payload.toString())).build();
var response = httpClient.send(request, BodyHandlers.ofString());
Api.requireSuccess(response);
AdminOutputModel admin = new Gson().fromJson(response.body(), AdminOutputModel.class);
var adminUserId = admin.Id;
// Use a token authorized to manage adminUserId, such as superAdminToken.
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", superAdminToken);
var systemOccurrences = await httpClient.GetFromJsonAsync<List<SystemOccurrenceOutputModel>>("api/v1/system-occurrence");
// Get PORTFOLIO-ORDER-SUBMISSION-DELIVERED system notification ID
var systemOccurrenceId = systemOccurrences.First(x => x.Code == "PORTFOLIO-ORDER-SUBMISSION-DELIVERED").Id;
var selectedSystemNotifications = new List<SystemOccurrencePreferenceInputModel>();
selectedSystemNotifications.Add(new SystemOccurrencePreferenceInputModel
{
Id = systemOccurrenceId,
IsEmailEnabled = false,
IsHttpEnabled = true, // HTTP callback
IsSmsEnabled = false
});
var payLoadAsString = JsonConvert.SerializeObject(selectedSystemNotifications);
using var response = await httpClient.PostAsync($"api/v1/system-occurrence/user/{adminUserId}",
new StringContent(payLoadAsString, Encoding.UTF8, "application/json"));
response.EnsureSuccessStatusCode();
HttpRequest eventsRequest = HttpRequest.newBuilder()
.uri(Settings.wmsUri("system-occurrence"))
.timeout(Duration.ofMinutes(1))
.header("Authorization", "Bearer " + Settings.SUPERADMIN_TOKEN).GET().build();
var eventsResponse = httpClient.send(eventsRequest, BodyHandlers.ofString());
Api.requireSuccess(eventsResponse);
Type eventListType = new TypeToken<List<SystemOccurrenceOutputModel>>() {}.getType();
List<SystemOccurrenceOutputModel> events = new Gson().fromJson(eventsResponse.body(), eventListType);
var event = events.stream()
.filter(x -> "PORTFOLIO-ORDER-SUBMISSION-DELIVERED".equals(x.Code))
.findFirst().orElseThrow(() -> new IllegalStateException("Event unavailable in this environment"));
JsonObject preference = new JsonObject();
preference.addProperty("Id", event.Id);
preference.addProperty("IsEmailEnabled", false);
preference.addProperty("IsSmsEnabled", false);
preference.addProperty("IsHttpEnabled", true);
JsonArray preferences = new JsonArray();
preferences.add(preference);
HttpRequest subscriptionRequest = HttpRequest.newBuilder()
.uri(Settings.wmsUri("system-occurrence/user/" + adminUserId))
.timeout(Duration.ofMinutes(1))
.header("Authorization", "Bearer " + Settings.SUPERADMIN_TOKEN)
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(preferences.toString())).build();
var subscriptionResponse = httpClient.send(subscriptionRequest, BodyHandlers.ofString());
Api.requireSuccess(subscriptionResponse);