// [START gae_flex_datastore_app]
'use strict';
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const express_1 = __importDefault(require("express"));
const body_parser_1 = __importDefault(require("body-parser"));
const app = (0, express_1.default)();
app.enable('trust proxy');
app.use((0, body_parser_1.default)());
// By default, the client will authenticate using the service account file
// specified by the GOOGLE_APPLICATION_CREDENTIALS environment variable and use
// the project specified by the GOOGLE_CLOUD_PROJECT environment variable. See
// https://github.com/GoogleCloudPlatform/google-cloud-node/blob/master/docs/authentication.md
// These environment variables are set automatically on Google App Engine
const datastore_1 = require("@google-cloud/datastore");
const axios_1 = __importDefault(require("axios"));
// Instantiate a datastore client
const datastore = new datastore_1.Datastore();
async function getOne(datastore, key) {
    const array = await datastore.get(key);
    if (!array[0])
        return;
    return array[0];
}
async function putOne(datastore, key, data) {
    return await datastore.save({
        key,
        data,
        excludeFromIndexes: ['endpoint'],
    });
}
const userIds = new Map();
app.post('/event', async (req, res) => {
    res.send('ok');
    try {
        // the actual payload with the user id is in a base64 data blob.
        const payload = JSON.parse(Buffer.from(req.body.message.data, 'base64').toString());
        const { userId } = payload;
        if (!userId) {
            console.error('no userId found');
            return;
        }
        let endpoint = userIds.get(userId);
        if (!endpoint) {
            const key = datastore.key(['UserIdEndpoint', userId]);
            endpoint = await getOne(datastore, key);
            if (!endpoint) {
                endpoint = {
                    endpoint: undefined,
                };
                putOne(datastore, key, endpoint);
            }
            userIds.set(userId, endpoint);
        }
        if (!endpoint.endpoint) {
            console.error('unmapped userId');
            return;
        }
        axios_1.default.post(endpoint.endpoint, req.body).catch(() => { });
    }
    catch (e) {
        console.error('payload error', e);
    }
});
app.post('/register/:userId', async (req, res) => {
    const { userId } = req.params;
    const { endpoint } = req.body;
    console.log(userId, endpoint);
    let check = userIds.get(userId);
    const key = datastore.key(['UserIdEndpoint', userId]);
    if (!check) {
        // found but no mapping
        if (userIds.has(userId)) {
            res.status(404);
            res.send({});
            return;
        }
        check = await getOne(datastore, key);
        userIds.set(userId, check);
    }
    // must exist to persist
    if (check) {
        if (check.endpoint !== endpoint) {
            check = {
                endpoint,
            };
            await putOne(datastore, key, check);
            userIds.set(userId, check);
        }
        res.status(200);
        res.send({});
        return;
    }
    res.status(404);
    res.send({});
});
const PORT = process.env.PORT || 8080;
app.listen(process.env.PORT || 8080, () => {
    console.log(`App listening on port ${PORT}`);
    console.log('Press Ctrl+C to quit.');
});
// [END gae_flex_datastore_app]
module.exports = app;
//# sourceMappingURL=app.js.map