Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement mint legacy api endpoints #2201

Merged
merged 8 commits into from
May 31, 2024
5 changes: 5 additions & 0 deletions config/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,11 @@ module.exports.routes = {
action: 'harvest',
csrf: false
},
'post /:branding/:portal/api/mint/harvest/:recordType': {
controller: 'webservice/RecordController',
action: 'legacyHarvest',
csrf: false
},
'put /:branding/:portal/api/records/objectmetadata/:oid': {
controller: 'webservice/RecordController',
action: 'updateObjectMeta',
Expand Down
1 change: 0 additions & 1 deletion config/workflow.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,5 +283,4 @@ module.exports.workflow = {
consolidated: true
}
}

};
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
meta {
name: Create HARVEST Legacy Record
type: http
seq: 32
}

post {
url: {{host}}/default/rdmp/api/mint/harvest/rdmp
body: json
auth: none
}

headers {
Content-Type: application/json
Authorization: Bearer {{token}}
}

body:json {
{
"records": [
{
"harvest_id": "s123456",
"metadata": {
"data": {
"ID": "s123456",
"GIVEN_NAME": "Andrew",
"OTHER_NAMES": "",
"FAMILY_NAME": "S",
"PREF_NAME": "",
"HONORIFIC": "",
"EMAIL": "[email protected]",
"JOB_TITLE": "Assistant Professor",
"URI": "",
"NLA_PARTY_IDENTIFIER": "",
"ORCID": "0000-0001-7269-2286",
"PERSONAL_HOMEPAGE": "",
"STAFF_PROFILE_HOMEPAGE": "",
"DESCRIPTION": "",
"RESEARCH_ELEMENTS_USER_ID": "12345678"
}
}
}
]
}
}

tests {

test("Status code is 200", function () {
expect(res.getStatus()).to.equal(200);
});

test("Test oid exists", function () {
var jsonData = res.getBody();
expect(jsonData[0]).to.have.property('oid');
});

test("Test harvestId exists and value is as expected", function () {
var jsonData = res.getBody();
expect(jsonData[0]).to.have.property('harvestId');
expect(jsonData[0].harvestId).to.equal('s123456');
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
meta {
name: Create HARVEST Record
type: http
seq: 33
}

post {
url: {{host}}/default/rdmp/api/records/harvest/rdmp
body: json
auth: none
}

headers {
Content-Type: application/json
Authorization: Bearer {{token}}
}

body:json {
{
"records": [
{
"harvestId": "s123456",
"recordRequest": {
"metadata": {
"ID": "s123456",
"GIVEN_NAME": "Andrew",
"OTHER_NAMES": "",
"FAMILY_NAME": "S",
"PREF_NAME": "",
"HONORIFIC": "",
"EMAIL": "[email protected]",
"JOB_TITLE": "Assistant Professor",
"URI": "",
"NLA_PARTY_IDENTIFIER": "",
"ORCID": "0000-0001-7269-2286",
"PERSONAL_HOMEPAGE": "",
"STAFF_PROFILE_HOMEPAGE": "",
"DESCRIPTION": "",
"RESEARCH_ELEMENTS_USER_ID": "12345678"
}
}
}
]
}
}

tests {

test("Status code is 200", function () {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add some tests that check the response body is structured correctly?

expect(res.getStatus()).to.equal(200);
});

test("Test oid exists", function () {
var jsonData = res.getBody();
expect(jsonData[0]).to.have.property('oid');
});

test("Test harvestId exists and value is as expected", function () {
var jsonData = res.getBody();
expect(jsonData[0]).to.have.property('harvestId');
expect(jsonData[0].harvestId).to.equal('s123456');
});

}
129 changes: 75 additions & 54 deletions typescript/api/controllers/webservice/RecordController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,11 @@
DatastreamServiceResponse,
RecordModel,
RecordsService,
SearchService
SearchService,
ListAPIResponse,
RecordTypeModel,
UserModel
} from '@researchdatabox/redbox-core-types';
import { ListAPIResponse } from '@researchdatabox/redbox-core-types';



Expand Down Expand Up @@ -91,7 +93,8 @@
'removeRoleEdit',
'addRoleView',
'removeRoleView',
'harvest'
'harvest',
'legacyHarvest'
];

constructor() {
Expand Down Expand Up @@ -440,7 +443,7 @@
if (response.isSuccessful()) {

if(workflowStage) {
WorkflowStepsService.get(recordTypeModel.name, workflowStage).subscribe(wfStep => {
WorkflowStepsService.get(recordTypeModel, workflowStage).subscribe(wfStep => {

Check warning on line 446 in typescript/api/controllers/webservice/RecordController.ts

View check run for this annotation

Codecov / codecov/patch

typescript/api/controllers/webservice/RecordController.ts#L446

Added line #L446 was not covered by tests
that.RecordsService.updateWorkflowStep(request, wfStep);
});
}
Expand Down Expand Up @@ -1003,13 +1006,13 @@
const brand:BrandingModel = BrandingService.getBrand(req.session.branding);
const oid = req.param('oid');
const body = req.body;
const users = body["roles"];
const users = body['roles'];

let record;
try {
record = await this.RecordsService.getMeta(oid);
if (users != null && users.length > 0) {
record["authorization"]["viewRoles"] = _.difference(record["authorization"]["viewRoles"], users);
record['authorization']['viewRoles'] = _.difference(record['authorization']['viewRoles'], users);
}
} catch (err) {
return this.apiFailWrapper(req, res, 500, null, err,
Expand All @@ -1022,8 +1025,8 @@
return this.apiFailWrapper(req, res, 500, null, null,
`Failed to update record with oid ${oid}, check server logs.`);
}
const resultRecord = await this.RecordsService.getMeta(result["oid"]);
return res.json(resultRecord["authorization"]);
const resultRecord = await this.RecordsService.getMeta(result['oid']);
return res.json(resultRecord['authorization']);
} catch (err) {
return this.apiFailWrapper(req, res, 500, null, err,
'Failed getting record meta for removing a viewer role, check server logs.');
Expand All @@ -1032,35 +1035,32 @@

public async harvest(req, res) {
const brand:BrandingModel = BrandingService.getBrand(req.session.branding);
let updateModes = ["merge", "override", "create"]
let updateModes = ["merge", "override", "create"];

let updateMode = req.param('updateMode')
if (_.isEmpty(updateMode)) {
updateMode = "override"
updateMode = "override";
}

var recordType = req.param('recordType');
var recordTypeModel = await RecordTypesService.get(brand, recordType).toPromise();

var recordTypeModel:RecordTypeModel = await RecordTypesService.get(brand, recordType).toPromise();

if (recordTypeModel == null) {
return this.apiFailWrapper(req, res, 400,null, null,
"Record Type provided is not valid");
return this.apiFailWrapper(req, res, 400,null, null, "Record Type provided is not valid");

Check warning on line 1049 in typescript/api/controllers/webservice/RecordController.ts

View check run for this annotation

Codecov / codecov/patch

typescript/api/controllers/webservice/RecordController.ts#L1049

Added line #L1049 was not covered by tests
}
var user = req.user;
var body = req.body;
if (body != null) {

if (_.isEmpty(body["records"])) {
return this.apiFailWrapper(req, res, 400, null, null,
"Invalid request body");
return this.apiFailWrapper(req, res, 400, null, null, "Invalid request body");

Check warning on line 1056 in typescript/api/controllers/webservice/RecordController.ts

View check run for this annotation

Codecov / codecov/patch

typescript/api/controllers/webservice/RecordController.ts#L1056

Added line #L1056 was not covered by tests
}
let recordResponses = [];
let records = body['records'];
for (let record of records) {
let harvestId = record["harvestId"]
if (_.isEmpty(harvestId)) {
recordResponses.push(new APIHarvestResponse(harvestId, null, false, "HarvestId was not specified"))
recordResponses.push(new APIHarvestResponse(harvestId, null, false, "HarvestId was not specified"));

Check warning on line 1063 in typescript/api/controllers/webservice/RecordController.ts

View check run for this annotation

Codecov / codecov/patch

typescript/api/controllers/webservice/RecordController.ts#L1063

Added line #L1063 was not covered by tests
} else {
let existingRecord = await this.findExistingHarvestRecord(harvestId, recordType)
if (existingRecord.length == 0 || updateMode == "create") {
Expand All @@ -1077,17 +1077,53 @@
}
return res.json(recordResponses);
}
return this.apiFailWrapper(req, res, 400, null, null,
"Invalid request");
return this.apiFailWrapper(req, res, 400, null, null, "Invalid request");

Check warning on line 1080 in typescript/api/controllers/webservice/RecordController.ts

View check run for this annotation

Codecov / codecov/patch

typescript/api/controllers/webservice/RecordController.ts#L1080

Added line #L1080 was not covered by tests
}

private async updateHarvestRecord(brand: any, recordTypeModel: any, updateMode: any, body: any, oid: any, harvestId: any, user: any) {
public async legacyHarvest(req, res) {
const brand:BrandingModel = BrandingService.getBrand(req.session.branding);

var recordType = req.param('recordType');
var recordTypeModel:RecordTypeModel = await RecordTypesService.get(brand, recordType).toPromise();

if (recordTypeModel == null) {
return this.apiFailWrapper(req, res, 400,null, null, 'Record Type provided is not valid');

Check warning on line 1090 in typescript/api/controllers/webservice/RecordController.ts

View check run for this annotation

Codecov / codecov/patch

typescript/api/controllers/webservice/RecordController.ts#L1090

Added line #L1090 was not covered by tests
}
var user = req.user;
var body = req.body;
if (body != null) {

if (_.isEmpty(body['records'])) {
return this.apiFailWrapper(req, res, 400, null, null, 'Invalid request body');

Check warning on line 1097 in typescript/api/controllers/webservice/RecordController.ts

View check run for this annotation

Codecov / codecov/patch

typescript/api/controllers/webservice/RecordController.ts#L1097

Added line #L1097 was not covered by tests
}
let recordResponses = [];
let records = body['records'];
for (let record of records) {
let harvestId = record['harvest_id'];
if (_.isEmpty(harvestId)) {
recordResponses.push(new APIHarvestResponse(harvestId, null, false, 'HarvestId was not specified'));

Check warning on line 1104 in typescript/api/controllers/webservice/RecordController.ts

View check run for this annotation

Codecov / codecov/patch

typescript/api/controllers/webservice/RecordController.ts#L1104

Added line #L1104 was not covered by tests
} else {
let existingRecord = await this.findExistingHarvestRecord(harvestId, recordType);
if (existingRecord.length == 0) {
recordResponses.push(await this.createHarvestRecord(brand, recordTypeModel, record['metadata']['data'], harvestId, 'update', user));
} else {
let oid = existingRecord[0].redboxOid;
recordResponses.push(await this.updateHarvestRecord(brand, recordTypeModel, 'update', record['metadata']['data'], oid, harvestId, user));

Check warning on line 1111 in typescript/api/controllers/webservice/RecordController.ts

View check run for this annotation

Codecov / codecov/patch

typescript/api/controllers/webservice/RecordController.ts#L1110-L1111

Added lines #L1110 - L1111 were not covered by tests
}
}
}
return res.json(recordResponses);
}
return this.apiFailWrapper(req, res, 400, null, null, 'Invalid request');

Check warning on line 1117 in typescript/api/controllers/webservice/RecordController.ts

View check run for this annotation

Codecov / codecov/patch

typescript/api/controllers/webservice/RecordController.ts#L1117

Added line #L1117 was not covered by tests
}

private async updateHarvestRecord(brand: BrandingModel, recordTypeModel: RecordTypeModel, updateMode: string, body: any, oid: string, harvestId: string, user: UserModel) {

const shouldMerge = updateMode == "merge" ? true : false;
try {
let record = await this.RecordsService.getMeta(oid)
let record:RecordModel = await this.RecordsService.getMeta(oid);
if (_.isEmpty(record)) {
return new APIHarvestResponse(harvestId, oid, false, `Failed to update meta, cannot find existing record with oid: ${oid}`)
return new APIHarvestResponse(harvestId, oid, false, `Failed to update meta, cannot find existing record with oid: ${oid}`);

Check warning on line 1126 in typescript/api/controllers/webservice/RecordController.ts

View check run for this annotation

Codecov / codecov/patch

typescript/api/controllers/webservice/RecordController.ts#L1126

Added line #L1126 was not covered by tests
}
try {
if (shouldMerge) {
Expand Down Expand Up @@ -1121,8 +1157,7 @@
}
}


private async findExistingHarvestRecord(harvestId: any, recordType: any) {
private async findExistingHarvestRecord(harvestId: string, recordType: string) {
let results = await Record.find({
'harvestId': harvestId,
'metaMetadata.type': recordType
Expand All @@ -1132,56 +1167,42 @@
return results;
}

private async createHarvestRecord(brand, recordTypeModel, body, harvestId, updateMode, user) {
private async createHarvestRecord(brand: BrandingModel, recordTypeModel: RecordTypeModel, body: any, harvestId: string, updateMode: string, user: UserModel) {
let authorizationEdit, authorizationView, authorizationEditPending, authorizationViewPending;
if (body["authorization"] != null) {
authorizationEdit = body["authorization"]["edit"];
authorizationView = body["authorization"]["view"];
authorizationEditPending = body["authorization"]["editPending"];
authorizationViewPending = body["authorization"]["viewPending"];
if (body['authorization'] != null) {
authorizationEdit = body['authorization']['edit'];
authorizationView = body['authorization']['view'];
authorizationEditPending = body['authorization']['editPending'];
authorizationViewPending = body['authorization']['viewPending'];

Check warning on line 1176 in typescript/api/controllers/webservice/RecordController.ts

View check run for this annotation

Codecov / codecov/patch

typescript/api/controllers/webservice/RecordController.ts#L1173-L1176

Added lines #L1173 - L1176 were not covered by tests
} else {
// If no authorization block set to user
body["authorization"] = [];
body['authorization'] = [];
authorizationEdit = [];
authorizationView = [];
authorizationEdit.push(user.username);
authorizationView.push(user.username);
}

let sourceMetadata = body["sourceMetadata"];

var metadata = body["metadata"];
var workflowStage = body["workflowStage"];
var metadata = body['metadata'];
var workflowStage = body['workflowStage'];
var request = {};
if (updateMode != "create") {
if (updateMode != 'create') {
// Only set harvestId if not in create mode
request["harvestId"] = harvestId;
}
var metaMetadata = {};
metaMetadata["brandId"] = brand.id;
metaMetadata["type"] = recordTypeModel.name;
metaMetadata["packageName"] = recordTypeModel.packageName;
metaMetadata["packageType"] = recordTypeModel.packageType;

if (!_.isEmpty(sourceMetadata)) {
//Force this to be stored as a string
metaMetadata["sourceMetadata"] = "" + sourceMetadata
request['harvestId'] = harvestId;
}
// Resolves #723: removed hardcoded value
metaMetadata["createdBy"] = user.username;
request["metaMetadata"] = metaMetadata;

//if no metadata field, no authorization
if (metadata == null) {
request["metadata"] = body;
request['metadata'] = body;
} else {
request["metadata"] = metadata;
request['metadata'] = metadata;

Check warning on line 1198 in typescript/api/controllers/webservice/RecordController.ts

View check run for this annotation

Codecov / codecov/patch

typescript/api/controllers/webservice/RecordController.ts#L1198

Added line #L1198 was not covered by tests
}

try {
let response = await this.RecordsService.create(brand, request, recordTypeModel, user)
let response = await this.RecordsService.create(brand, request, recordTypeModel, user);

if(workflowStage) {
let wfStep = await WorkflowStepsService.get(recordTypeModel.name, workflowStage).toPromise();
let wfStep = await WorkflowStepsService.get(recordTypeModel, workflowStage).toPromise();

Check warning on line 1205 in typescript/api/controllers/webservice/RecordController.ts

View check run for this annotation

Codecov / codecov/patch

typescript/api/controllers/webservice/RecordController.ts#L1205

Added line #L1205 was not covered by tests
this.RecordsService.updateWorkflowStep(request, wfStep);
}

Expand Down
2 changes: 1 addition & 1 deletion typescript/api/services/ReportsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@
return this.getTranslateDatabaseResultToReportResult(dbResult, report);
} else {
let url = this.buildSolrParams(brand, req, report, start, rows, 'json');
const solrResults = await this.getSearchService().searchAdvanced(report.solrQuery.searchCore,null, url); //TODO pass type
const solrResults = await this.getSearchService().searchAdvanced(report.solrQuery.searchCore,null, url);

Check warning on line 176 in typescript/api/services/ReportsService.ts

View check run for this annotation

Codecov / codecov/patch

typescript/api/services/ReportsService.ts#L176

Added line #L176 was not covered by tests
return this.getTranslateSolrResultToReportResult(solrResults, rows);
}
}
Expand Down
Loading