Skip to content
This repository was archived by the owner on Apr 22, 2024. It is now read-only.

Commit aec1bf6

Browse files
committed
Count registered publication only
1 parent 2c50555 commit aec1bf6

File tree

2 files changed

+95
-43
lines changed

2 files changed

+95
-43
lines changed

common/models/published-data.js

Lines changed: 56 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -396,49 +396,8 @@ module.exports = function (PublishedData) {
396396
});
397397

398398
PublishedData.beforeRemote("find", function (ctx, unused, next) {
399-
const accessToken = ctx.args.options.accessToken;
400-
if (!accessToken) {
401-
let filter = {};
402-
if (ctx.args.filter) {
403-
if (ctx.args.filter.where) {
404-
filter.where = {};
405-
if (ctx.args.filter.where.and) {
406-
filter.where.and = [];
407-
filter.where.and.push({ status: "registered" });
408-
filter.where.and = filter.where.and.concat(
409-
ctx.args.filter.where.and
410-
);
411-
} else if (ctx.args.filter.where.or) {
412-
filter.where.and = [];
413-
filter.where.and.push({ status: "registered" });
414-
filter.where.and.push({ or: ctx.args.filter.where.or });
415-
} else {
416-
filter.where = {
417-
and: [{ status: "registered" }].concat(
418-
ctx.args.filter.where
419-
),
420-
};
421-
}
422-
} else {
423-
filter.where = { status: "registered" };
424-
}
425-
if (ctx.args.filter.skip) {
426-
filter.skip = ctx.args.filter.skip;
427-
}
428-
if (ctx.args.filter.limit) {
429-
filter.limit = ctx.args.filter.limit;
430-
}
431-
if (ctx.args.filter.include) {
432-
filter.include = ctx.args.filter.include;
433-
}
434-
if (ctx.args.filter.fields) {
435-
filter.fields = ctx.args.filter.fields;
436-
}
437-
} else {
438-
filter = { where: { status: "registered" } };
439-
}
440-
ctx.args.filter = filter;
441-
}
399+
const filter = addRegisteredIfUnathenticated(ctx, ctx.args.filter);
400+
if (filter) ctx.args.filter = filter;
442401
next();
443402
});
444403

@@ -455,4 +414,58 @@ module.exports = function (PublishedData) {
455414
// PublishedData.beforeRemote('patchOrCreate', function(ctx, instance, next) {
456415
// next();
457416
// });
417+
418+
PublishedData.beforeRemote("count", function (ctx, unused, next) {
419+
const filter = addRegisteredIfUnathenticated(ctx, ctx.args);
420+
if (filter) ctx.args.where = filter.where;
421+
next();
422+
});
458423
};
424+
425+
function addRegisteredIfUnathenticated(ctx, filterField) {
426+
const filterClone = JSON.parse(JSON.stringify(filterField));
427+
const accessToken = ctx.args.options.accessToken;
428+
if (!accessToken) {
429+
let filter = {};
430+
if (filterClone) {
431+
if (filterClone.where) {
432+
filter.where = {};
433+
if (filterClone.where.and) {
434+
filter.where.and = [];
435+
filter.where.and.push({ status: "registered" });
436+
filter.where.and = filter.where.and.concat(
437+
filterClone.where.and
438+
);
439+
} else if (filterClone.or) {
440+
filter.where.and = [];
441+
filter.where.and.push({ status: "registered" });
442+
filter.where.and.push({ or: filterClone.where.or });
443+
} else {
444+
filter.where = {
445+
and: [{ status: "registered" }].concat(
446+
filterClone.where
447+
),
448+
};
449+
}
450+
} else {
451+
filter.where = { status: "registered" };
452+
}
453+
if (filterClone.skip) {
454+
filter.skip = filterClone.skip;
455+
}
456+
if (filterClone.limit) {
457+
filter.limit = filterClone.limit;
458+
}
459+
if (filterClone.include) {
460+
filter.include = filterClone.include;
461+
}
462+
if (filterClone.fields) {
463+
filter.fields = filterClone.fields;
464+
}
465+
} else {
466+
filter = { where: { status: "registered" } };
467+
}
468+
return filter;
469+
}
470+
}
471+

test/PublishedData.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,32 @@ describe("Test of access to published data", () => {
180180
});
181181
});
182182

183+
it("should return the registered publisheddata count", function (done) {
184+
request(app)
185+
.get("/api/v3/PublishedData/count")
186+
.set("Accept", "application/json")
187+
.expect(200)
188+
.expect("Content-Type", /json/)
189+
.end((err, res) => {
190+
if (err) return done(err);
191+
res.body.count.should.be.equal(0);
192+
done();
193+
});
194+
});
195+
196+
it("should return all the publisheddata count", function (done) {
197+
request(app)
198+
.get("/api/v3/PublishedData/count" + "?access_token=" + accessToken)
199+
.set("Accept", "application/json")
200+
.expect(200)
201+
.expect("Content-Type", /json/)
202+
.end((err, res) => {
203+
if (err) return done(err);
204+
res.body.count.should.be.equal(1);
205+
done();
206+
});
207+
});
208+
183209
it("should register this new published data", function (done) {
184210
nock("http://127.0.0.1:3000")
185211
.post("/api/v3/PublishedData/" + doi + "/register")
@@ -217,6 +243,19 @@ describe("Test of access to published data", () => {
217243
});
218244
});
219245

246+
it("should return the registered publisheddata count", function (done) {
247+
request(app)
248+
.get("/api/v3/PublishedData/count")
249+
.set("Accept", "application/json")
250+
.expect(200)
251+
.expect("Content-Type", /json/)
252+
.end((err, res) => {
253+
if (err) return done(err);
254+
res.body.count.should.be.equal(1);
255+
done();
256+
});
257+
});
258+
220259
// actual test
221260
/*it("should resync this new published data", function (done) {
222261
request(app)

0 commit comments

Comments
 (0)