// Identity. Store your memories and mental belongings
// Copyright (C) 2024  SofĂ­a Aritz
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.


import { contentFromSigned, verifySignature } from "../../m2m.js";

export default function register(app, auth) {
    app.put("/m2m/asset", {
        async handler(request, reply) {
            if (!verifySignature(request.body)) {
                reply.statusCode(401);
                return;
            }

            let body = JSON.parse(contentFromSigned(request.body));

            let user = await auth.findUserBySessionKey(body.session_key);
            user.assets.push(body.asset_id);

            await auth.updateUser(user.uid, user);
            app.log.info((await auth.findUserBySessionKey(body.session_key)).assets);
        },
    });
}