-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompanies.js
124 lines (102 loc) · 3.21 KB
/
companies.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
"use strict";
/** Routes for companies. */
const jsonschema = require("jsonschema");
const express = require("express");
const { BadRequestError } = require("../expressError");
const { ensureAdmin } = require("../middleware/auth");
const Company = require("../models/company");
const companyNewSchema = require("../schemas/companyNew.json");
const companyUpdateSchema = require("../schemas/companyUpdate.json");
const companySearchSchema = require("../schemas/companySearch.json");
const router = new express.Router();
/** POST / { company } => { company }
*
* company should be { handle, name, description, numEmployees, logoUrl }
*
* Returns { handle, name, description, numEmployees, logoUrl }
*
* Authorization required: admin
*/
router.post("/", ensureAdmin, async function (req, res, next) {
const validator = jsonschema.validate(
req.body,
companyNewSchema,
{required: true}
);
if (!validator.valid) {
const errs = validator.errors.map(e => e.stack);
throw new BadRequestError(errs);
}
const company = await Company.create(req.body);
return res.status(201).json({ company });
});
/** GET / =>
* { companies: [ { handle, name, description, numEmployees, logoUrl }, ...] }
*
* Can filter on provided search filters:
* - minEmployees
* - maxEmployees
* - nameLike (will find case-insensitive, partial matches)
*
* Authorization required: none
*/
router.get("/", async function (req, res, next) {
const q = req.query;
// arrive as strings from querystring, but we want as ints
if (q.minEmployees !== undefined) q.minEmployees = +q.minEmployees;
if (q.maxEmployees !== undefined) q.maxEmployees = +q.maxEmployees;
const validator = jsonschema.validate(
q,
companySearchSchema,
{required: true}
);
if (!validator.valid) {
const errs = validator.errors.map(e => e.stack);
throw new BadRequestError(errs);
}
const companies = await Company.findAll(q);
return res.json({ companies });
});
/** GET /[handle] => { company }
*
* Company is { handle, name, description, numEmployees, logoUrl, jobs }
* where jobs is [{ id, title, salary, equity }, ...]
*
* Authorization required: none
*/
router.get("/:handle", async function (req, res, next) {
const company = await Company.get(req.params.handle);
return res.json({ company });
});
/** PATCH /[handle] { fld1, fld2, ... } => { company }
*
* Patches company data.
*
* fields can be: { name, description, numEmployees, logo_url }
*
* Returns { handle, name, description, numEmployees, logo_url }
*
* Authorization required: admin
*/
router.patch("/:handle", ensureAdmin, async function (req, res, next) {
const validator = jsonschema.validate(
req.body,
companyUpdateSchema,
{required: true}
);
if (!validator.valid) {
const errs = validator.errors.map(e => e.stack);
throw new BadRequestError(errs);
}
const company = await Company.update(req.params.handle, req.body);
return res.json({ company });
});
/** DELETE /[handle] => { deleted: handle }
*
* Authorization: admin
*/
router.delete("/:handle", ensureAdmin, async function (req, res, next) {
await Company.remove(req.params.handle);
return res.json({ deleted: req.params.handle });
});
module.exports = router;