Skip to content

Instantly share code, notes, and snippets.

@bibscy
Created July 17, 2019 09:03
Show Gist options
  • Save bibscy/d94e8695bfa5e1c9fc16f0081eaaf2d2 to your computer and use it in GitHub Desktop.
Save bibscy/d94e8695bfa5e1c9fc16f0081eaaf2d2 to your computer and use it in GitHub Desktop.
//
// extensionNotificationsMyStreet.swift
// App
//
// Created by Bogdan Barbulescu on 10/08/2018.
//
import Foundation
import Vapor
import HTTP
struct DataFromClientSendNotifications {
let title: String
let type: String
let body: String
let chatUID: String
let sound: String
let badge: String
let fcmToken: String
let unreadMessagesCount: Int
let createdAt: Int
let isDevelopment: Bool
}
extension NotificationsMyStreet {
//assign the data received from cleanerApp to global variables in order to use them inside the class
func prepareVars(request:Request) throws -> [DataFromClientSendNotifications]
{
guard let reqJson = request.json else {
let message = "data not received as JSON"
logErr.prints(message: message)
drop.log.error(message)
throw Abort.custom(status: .badRequest, message: message)
}
var arrayOfNotifications = [DataFromClientSendNotifications]()
for obj in reqJson.object! {
let objValue = obj.value.object
guard let title = objValue?["title"]?.string else {
print("title is missing")
throw Abort.custom(status: .badRequest, message: "title is missing")
}
guard let type = objValue?["type"]?.string else {
print("type is missing")
throw Abort.custom(status: .badRequest, message: "type is missing")
}
guard let body = objValue?["body"]?.string else {
print("body is missing")
throw Abort.custom(status: .badRequest, message: "body is missing")
}
var chatID = ""
if let chatUID = objValue?["chatUID"]?.string {
chatID = chatUID
}
guard let sound = objValue?["sound"]?.string else {
print("sound is missing")
throw Abort.custom(status: .badRequest, message: "sound is missing")
}
guard let badge = objValue?["badge"]?.string else {
print("badge is missing")
throw Abort.custom(status: .badRequest, message: "badge is missing")
}
guard let fcmToken = objValue?["fcmToken"]?.string else {
print("fcmToken is missing")
throw Abort.custom(status: .badRequest, message: "fcmToken is missing")
}
var unreadMessagesCount:Int!
if let unreadMessages = objValue?["unreadMessagesCount"]?.int {
unreadMessagesCount = unreadMessages
}
guard let createdAt = objValue?["createdAt"]?.int else {
print("createdAt is missing")
throw Abort.custom(status: .badRequest, message: "createdAt is missing")
}
guard let isDevelopment = objValue?["isDevelopment"]?.bool else {
print("isDevelopment is missing")
throw Abort.custom(status: .badRequest, message: "createdAt is missing")
}
let itemCompleted = DataFromClientSendNotifications(
title: title,
type: type,
body: body,
chatUID: chatID,
sound: sound,
badge: badge,
fcmToken: fcmToken,
unreadMessagesCount: unreadMessagesCount ?? 0,
createdAt: createdAt,
isDevelopment: isDevelopment)
arrayOfNotifications.append(itemCompleted)
}
if arrayOfNotifications.count < 1 {
throw Abort.custom(status: .badRequest, message: "array of notifications received is 0")
}
return arrayOfNotifications
}//end of prepareVars
}//end of NotificationsMyStreet
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment