import { makeError, replies } from '@oyst/utils' import Boom from 'boom' import Merchant from '../models/merchant' import MerchantAPI from '../apis/merchant' import { bearerRegex } from '../validators/headers' const { error, success } = replies const handler = async (req, reply) => { try { const matches = bearerRegex.exec(req.headers.authorization) if (!matches) { return reply(Boom.unauthorized('unauthorized')) } const merchant: Merchant = await MerchantAPI.checkApiKey(matches[1]) const url = req.headers['oyst-notification-url'] if (url) { await MerchantAPI.updateUrl(merchant.id, url) } success(await Merchant.findOrCreate(merchant), reply) } catch (err) { error(makeError(err), reply) } } export default { method: handler, assign: 'merchant' }