Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions server/db/url.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const mongoose = require('mongoose')

const schema = new mongoose.Schema({
company: String,
edition: String,
kind: String,
url: String
})

schema.index({ 'company': 1, 'edition': 1, 'kind': 1 }, { 'unique': true })

module.exports = mongoose.model('Url', schema)

1 change: 1 addition & 0 deletions server/resources/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ require('./promo-code')
require('./happy-hour')
require('./prize')
require('./connection')
require('./url')
27 changes: 27 additions & 0 deletions server/resources/url.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const Boom = require('@hapi/boom')
const log = require('../helpers/logger')
const server = require('../').hapi
const Url = require('../db/url')

server.method('url.get', get, {})

async function get(companyId, editionId) {
const filterAll = {
edition: editionId,
kind: 'all'
}
const filterCompanyConnections = {
company: companyId,
edition: editionId,
kind: 'company'
}

const all = await Url.findOne(filterAll)
if (!all) {
log.error({ err: 'not found all', edition: editionId }, 'error getting download url')
throw Boom.notFound('url not found')
}
const companyConnections = await Url.findOne(filterCompanyConnections)

return { all: all ? all.url : undefined, companyConnections: companyConnections ? companyConnections.url : undefined }
}
1 change: 1 addition & 0 deletions server/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ require('./promo-code')
require('./google')
require('./prize')
require('./connection')
require('./url')
32 changes: 32 additions & 0 deletions server/routes/url/handlers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const Joi = require('joi')
const render = require('../../views/url')
const log = require('../../helpers/logger')
const Boom = require('@hapi/boom')

exports = module.exports

exports.get = {
options: {
tags: ['api', 'company-endpoint'],
auth: {
strategies: ['default'],
scope: ['team', 'admin', 'company']
},
validate: {
params: Joi.object({
companyId: Joi.string().required().description('Id of the company')
}),
},
description: 'Gets a company download urls'
},
handler: async (request, h) => {
try {
const latestEdition = await request.server.methods.deck.getLatestEdition()
const downloadLinks = await request.server.methods.url.get(request.params.companyId, latestEdition.id)
return h.response(render(downloadLinks))
} catch (err) {
log.error({ err: err}, 'error getting company')
throw Boom.internal()
}
},
}
45 changes: 45 additions & 0 deletions server/routes/url/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const server = require('../../').hapi
const handlers = require('./handlers')

// server.route({
// method: 'POST',
// path: '/url',
// options: handlers.create.options,
// handler: handlers.create.handler
// })
//
// server.route({
// method: ['PUT', 'PATCH'],
// path: '/url/{id}',
// options: handlers.update.options,
// handler: handlers.update.handler
// })
//
// server.route({
// method: 'GET',
// path: '/url/{id}',
// options: handlers.get.options,
// handler: handlers.get.handler
// })

server.route({
method: 'GET',
path: '/company/{companyId}/url',
options: handlers.get.options,
handler: handlers.get.handler
})

// server.route({
// method: 'GET',
// path: '/url',
// options: handlers.list.options,
// handler: handlers.list.handler
// })
//
// server.route({
// method: 'DELETE',
// path: '/company-endpoint/{companyId}',
// options: handlers.remove.options,
// handler: handlers.remove.handler
// })
//
14 changes: 14 additions & 0 deletions server/views/url.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module.exports = function render (content) {
if (content instanceof Array) {
return content.map(renderObject)
}

return renderObject(content)
}

function renderObject (model) {
return {
all: model.all,
companyConnections: model.companyConnections
}
}
Loading