1 | const Joi = require('@hapi/joi');
|
2 | const { validate, prepare } = require('../../utils/options');
|
3 |
|
4 |
|
5 | const _id = Joi.number().min(1);
|
6 | const _relationship_type = Joi.string().min(1);
|
7 | const _data = Joi.object();
|
8 |
|
9 |
|
10 | module.exports = (options = {}) => {
|
11 | const { error } = validate(options);
|
12 | if (error) throw new Error(error.details[0].message);
|
13 |
|
14 | const { url, headers } = prepare(options);
|
15 |
|
16 | return {
|
17 | |
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 | list: (options = {}) => {
|
27 | const { error } = Joi.object({
|
28 | id: _id,
|
29 | relationship_type: _relationship_type.required()
|
30 | }).validate(options);
|
31 | if (error) throw new Error(error.details[0].message);
|
32 |
|
33 | const { id, relationship_type } = options;
|
34 |
|
35 | return {
|
36 | method: 'GET',
|
37 | url: `${url}/api/sunshine/${
|
38 | id
|
39 | ? `objects/records/${id}/relationships/${relationship_type}`
|
40 | : `relationships/records?type=${relationship_type}`
|
41 | }`,
|
42 | headers
|
43 | };
|
44 | },
|
45 |
|
46 | |
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 | create: (options = {}) => {
|
53 | const { error } = Joi.object({
|
54 | data: _data.required()
|
55 | }).validate(options);
|
56 | if (error) throw new Error(error.details[0].message);
|
57 |
|
58 | const { data } = options;
|
59 | return {
|
60 | method: 'POST',
|
61 | url: `${url}/api/sunshine/relationships/records`,
|
62 | headers,
|
63 | data
|
64 | };
|
65 | },
|
66 |
|
67 | |
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 | delete: (options = {}) => {
|
74 | const { error } = Joi.object({
|
75 | id: _id.required()
|
76 | }).validate(options);
|
77 | if (error) throw new Error(error.details[0].message);
|
78 |
|
79 | const { id } = options;
|
80 | return {
|
81 | method: 'DELETE',
|
82 | url: `${url}/api/sunshine/relationships/records/${id}`,
|
83 | headers
|
84 | };
|
85 | }
|
86 | };
|
87 | };
|