Created
February 28, 2016 05:15
-
-
Save ajaybeniwal/e9a813f6c5082b0506c4 to your computer and use it in GitHub Desktop.
Sample Express App to handle apple wallet generation and updation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This is sample gist to handle wallet updates and workflow of wallet | |
var express = require('express'); | |
var app = express(); | |
var bodyParser = require('body-parser'); | |
var mongoose = require('mongoose'); | |
mongoose.connect('url'); | |
var Schema = mongoose.Schema, | |
ObjectId = Schema.ObjectId; | |
var PassDeviceSchema = new Schema({ | |
passTypeIdentifier: String, | |
serialNumber: String | |
}) | |
var DeviceSchema = new Schema({ | |
author: ObjectId, | |
deviceId: String, | |
pushToken: String, | |
passes: [PassDeviceSchema] | |
}); | |
var PassSchema = new Schema({ | |
passTypeIdentifier: String, | |
serialNumber: String | |
}) | |
var LogTableSchema = new Schema({ | |
passesUpdatedSince: String, | |
deviceId: String, | |
message: String | |
}) | |
var DeviceModel = mongoose.model('Device', DeviceSchema); | |
var PassModel = mongoose.model('Pass', PassSchema); | |
var LogModel = mongoose.model('Log', LogTableSchema); | |
// configure the app to use bodyParser() | |
app.use(bodyParser.urlencoded({ | |
extended: true | |
})); | |
app.use(bodyParser.json()); | |
app.get('/v1/devices/:deviceLibraryIdentifier/registrations/:passTypeIdentifier', (req, res) => { | |
var _log = new LogModel(); | |
var currentDate = new Date() | |
_log.passesUpdatedSince = req.query.passesUpdatedSince; | |
_log.deviceId = req.params.deviceLibraryIdentifier; | |
_log.message = "Recieved push notifications"; | |
_log.save((err) => { | |
var data = { lastUpdated: currentDate.toDateString(), serialNumbers: ['E5982H-I2'] } | |
res.status(200).send(data); | |
}) | |
}) | |
app.get('/v1/passes/:passTypeIdentifie/:serialNumber', (req, res) => { | |
var options = { | |
root: __dirname, | |
headers: { | |
'x-timestamp': Date.now(), | |
'x-sent': true, | |
'Content-Type': 'application/vnd.apple.pkpass' | |
} | |
}; | |
res.sendFile('BoardinPass.pkpass', options, (err) => { | |
if (err) { | |
res.status(err.status).end(); | |
} | |
else { | |
console.log('Sent:', 'BoardinPass.pkpass'); | |
} | |
}) | |
}) | |
app.post('/v1/devices/:deviceLibraryIdentifier/registrations/:passTypeIdentifier/:serialNumber', function (req, res) { | |
console.log(req.params.deviceLibraryIdentifier); | |
var model = new DeviceModel(); | |
var _pass = new PassModel(); | |
_pass.passTypeIdentifier = req.params.passTypeIdentifier; | |
_pass.serialNumber = req.params.serialNumber; | |
model.deviceId = req.params.deviceLibraryIdentifier; | |
model.pushToken = req.body.pushToken | |
model.passes.push({ passTypeIdentifier: req.params.passTypeIdentifier, serialNumber: req.params.serialNumber }) | |
model.save((err) => { | |
_pass.save((err) => { | |
res.status(201).send(model.pushToken); | |
}) | |
}) | |
}) | |
app.delete('/v1/devices/:deviceLibraryIdentifier/registrations/:passTypeIdentifier/:serialNumber', function (req, res) { | |
PassModel.remove({ serialNumber: req.params.serialNumber }, (err) => { | |
res.status(201).send("pass removed successfully"); | |
}) | |
}) | |
var port = process.env.PORT || 1337; | |
app.listen(port, function () { | |
console.log('parse-server-example running on port ' + port + '.'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample Implementation of Wallet backend on the Node.js