-
Notifications
You must be signed in to change notification settings - Fork 15
/
publisherKey.ctrl.js
133 lines (126 loc) · 4.63 KB
/
publisherKey.ctrl.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
125
126
127
128
129
130
131
132
133
let express = require('express');
let Publisher = require('../../../models/gbifdata/gbifdata').Publisher;
let helper = rootRequire('app/models/util/util');
let utils = rootRequire('app/helpers/utils');
let contributors = require('../../dataset/key/contributors/contributors');
let authOperations = require('../../auth/gbifAuthRequest');
let apiConfig = rootRequire('app/models/gbifdata/apiConfig');
let log = require('../../../../config/log');
let router = express.Router({caseSensitive: true});
module.exports = function (app) {
app.use('/', router);
};
module.exports = function (app) {
app.use('/', router);
};
router.get('/publisher/confirm', confirm);
router.get('/publisher/:key.:ext?', render);
router.get('/publisher/:key/metrics', render);
function render(req, res, next) {
let key = req.params.key;
if (!utils.isGuid(key)) {
next();
} else {
Publisher.get(key).then(
function (publisher) {
try {
publisher._computedValues = {};
let contacts = publisher.record.contacts;
let organizationContact = {
organization: publisher.record.title,
city: publisher.record.city,
country: publisher.record.country,
address: publisher.record.address,
province: publisher.record.province,
email: publisher.record.email,
postalCode: publisher.record.postalCode
};
if (
organizationContact.address ||
organizationContact.email
) {
contacts.push(organizationContact);
}
publisher._computedValues.contributors = contributors.getContributors(
contacts
);
helper.renderPage(req, res, next,
{
publisher: publisher,
_meta: {
title: publisher.record.endorsementApproved
? publisher.record.title
: 'New publisher',
description: publisher.record
.endorsementApproved
? publisher.record.description
: '',
noIndex: !publisher.record.endorsementApproved || publisher.deleted,
}
},
'pages/publisher/key/seo'
);
} catch (error) {
next(error);
}
},
function (err) {
if (err.type == 'NOT_FOUND') {
next();
} else {
next(err);
}
}
);
}
}
async function confirm(req, res, next) {
let key = req.query.key;
let code = req.query.code;
let getOrganizationRequest = {
url: apiConfig.publisherCreate.url + key,
method: 'GET'
};
let opts = {
method: 'POST',
body: {confirmationKey: code},
url: apiConfig.publisherCreate.url + key + '/endorsement',
canonicalPath: apiConfig.publisherCreate.canonical + key + '/endorsement'
};
try {
const getOrgResp = await authOperations.authenticatedRequest(getOrganizationRequest);
if (!getOrgResp.body.endorsementApproved) {
const response = await authOperations.authenticatedRequest(opts);
if (response.statusCode !== 204) {
throw response;
}
return helper.renderPage(req, res, next,
{
publisherKey: key
},
'pages/custom/confirmEndorsement/confirmEndorsement'
);
} else {
log.error('Organization[' + key + '] already endorsed');
return helper.renderPage(req, res, next,
{
publisherKey: key
},
'pages/custom/confirmEndorsement/alreadyEndorsed'
);
}
} catch (err) {
log.error(
'Failed to confirm endorsement for organization[' +
key +
'] : ' +
err.body
);
return helper.renderPage(req, res, next,
{
publisherKey: key
},
'pages/custom/confirmEndorsement/invalidToken'
);
}
}