Skip to content

Commit

Permalink
merge ready
Browse files Browse the repository at this point in the history
  • Loading branch information
vsun757 committed Feb 26, 2024
1 parent dee3550 commit aad6dd4
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 94 deletions.
120 changes: 119 additions & 1 deletion app/repository/collection-index-repository.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
const { BadlyFormattedParameterError, MissingParameterError } = require('../exceptions');
const { BadlyFormattedParameterError, MissingParameterError, DuplicateIdError } = require('../exceptions');
const CollectionIndex = require('../models/collection-index-model');
const superagent = require('superagent');
const config = require('../config/config');
class CollectionIndexRepository {

constructor(model) {
this.model = model;
}

errors = {
badRequest: 'Bad request',
missingParameter: 'Missing required parameter',
badlyFormattedParameter: 'Badly formatted parameter',
duplicateId: 'Duplicate id',
notFound: 'Document not found',
hostNotFound: 'Host not found',
connectionRefused: 'Connection refused',
};

async retrieveAll(options) {
const collectionIndexes = await this.model.find()
.skip(options.offset)
Expand Down Expand Up @@ -34,5 +46,111 @@ class CollectionIndexRepository {
}

Check warning on line 46 in app/repository/collection-index-repository.js

View check run for this annotation

Codecov / codecov/patch

app/repository/collection-index-repository.js#L41-L46

Added lines #L41 - L46 were not covered by tests
}

async create(data) {
try {
// Create the document
const collectionIndex = new this.model(data);

if (collectionIndex.workspace.update_policy && collectionIndex.workspace.update_policy.automatic && !collectionIndex.workspace.update_policy.interval) {
collectionIndex.workspace.update_policy.interval = config.collectionIndex.defaultInterval;
}

Check warning on line 56 in app/repository/collection-index-repository.js

View check run for this annotation

Codecov / codecov/patch

app/repository/collection-index-repository.js#L55-L56

Added lines #L55 - L56 were not covered by tests

// Save the document in the database
const savedCollectionIndex = await collectionIndex.save();

return savedCollectionIndex;
} catch (err) {
if (err.name === 'MongoServerError' && err.code === 11000) {
// 11000 = Duplicate index
const error = new Error(this.errors.duplicateId);
throw error;
} else {
throw err;
}

Check warning on line 69 in app/repository/collection-index-repository.js

View check run for this annotation

Codecov / codecov/patch

app/repository/collection-index-repository.js#L68-L69

Added lines #L68 - L69 were not covered by tests
}
}


async updateFull(id, data) {
try {
if (!id) {
throw new MissingParameterError;
}

Check warning on line 78 in app/repository/collection-index-repository.js

View check run for this annotation

Codecov / codecov/patch

app/repository/collection-index-repository.js#L77-L78

Added lines #L77 - L78 were not covered by tests

const collectionIndex = await this.model.findOne({ "collection_index.id": id });

if (!collectionIndex) {
// Collection index not found
return null;
}

Check warning on line 85 in app/repository/collection-index-repository.js

View check run for this annotation

Codecov / codecov/patch

app/repository/collection-index-repository.js#L83-L85

Added lines #L83 - L85 were not covered by tests

// Copy data to found document
Object.assign(collectionIndex, data);

// Save the updated document
const savedCollectionIndex = await collectionIndex.save();

return savedCollectionIndex;
} catch (err) {
if (err.name === 'CastError') {
throw new BadlyFormattedParameterError;
} else if (err.name === 'MongoServerError' && err.code === 11000) {
throw new DuplicateIdError;
} else {
throw err;
}
}

Check warning on line 102 in app/repository/collection-index-repository.js

View check run for this annotation

Codecov / codecov/patch

app/repository/collection-index-repository.js#L95-L102

Added lines #L95 - L102 were not covered by tests
}


async delete(id) {
if (!id) {
const error = new Error(this.errors.missingParameter);
error.parameterName = 'id';
throw error;
}

Check warning on line 111 in app/repository/collection-index-repository.js

View check run for this annotation

Codecov / codecov/patch

app/repository/collection-index-repository.js#L108-L111

Added lines #L108 - L111 were not covered by tests

const collectionIndex = await this.model.findOneAndRemove({ "collection_index.id": id });

return collectionIndex; // Note: collectionIndex is null if not found
}


/**
* Retrieves a collection index from the provided URL.
* This is expected to be a remote URL that does not require authentication.
*/
async retrieveByUrl(url) {
try {
if (!url) {
const error = new Error(this.errors.missingParameter);
throw error;
}

const res = await superagent.get(url).set('Accept', 'application/json');

// Handle different error cases
if (res.notFound) {
const error = new Error(this.errors.notFound);
throw error;
} else if (res.badRequest) {
const error = new Error(this.errors.badRequest);
throw error;
}

// Parsing res.text handles both the content-type text/plain and application/json use cases
const collectionIndex = JSON.parse(res.text);
return collectionIndex;
} catch (err) {
if (err.code === 'ENOTFOUND') {
throw new Error(this.errors.hostNotFound);
} else if (err.code === 'ECONNREFUSED') {
throw new Error(this.errors.connectionRefused);
} else {
throw err;
}
}
}

Check warning on line 153 in app/repository/collection-index-repository.js

View check run for this annotation

Codecov / codecov/patch

app/repository/collection-index-repository.js#L124-L153

Added lines #L124 - L153 were not covered by tests

}
module.exports = new CollectionIndexRepository(CollectionIndex);
98 changes: 5 additions & 93 deletions app/services/collection-indexes-service.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
'use strict';

const superagent = require('superagent');

const CollectionIndex = require('../models/collection-index-model');
const config = require('../config/config');
const CollectionIndexRepository = require('../repository/collection-index-repository');
const { MissingParameterError, BadlyFormattedParameterError, DuplicateIdError } = require('../exceptions');

class CollectionIndexService {

Expand Down Expand Up @@ -36,72 +31,17 @@ class CollectionIndexService {


async create(data) {
try {
// Create the document
const collectionIndex = new CollectionIndex(data);

if (collectionIndex.workspace.update_policy && collectionIndex.workspace.update_policy.automatic && !collectionIndex.workspace.update_policy.interval) {
collectionIndex.workspace.update_policy.interval = config.collectionIndex.defaultInterval;
}

// Save the document in the database
const savedCollectionIndex = await collectionIndex.save();

return savedCollectionIndex;
} catch (err) {
if (err.name === 'MongoServerError' && err.code === 11000) {
// 11000 = Duplicate index
const error = new Error(this.errors.duplicateId);
throw error;
} else {
throw err;
}
}
return await this.repository.create(data);
}


async updateFull(id, data) {
try {
if (!id) {
throw new MissingParameterError;
}

const collectionIndex = await CollectionIndex.findOne({ "collection_index.id": id });

if (!collectionIndex) {
// Collection index not found
return null;
}

// Copy data to found document
Object.assign(collectionIndex, data);

// Save the updated document
const savedCollectionIndex = await collectionIndex.save();

return savedCollectionIndex;
} catch (err) {
if (err.name === 'CastError') {
throw new BadlyFormattedParameterError;
} else if (err.name === 'MongoServerError' && err.code === 11000) {
throw new DuplicateIdError;
} else {
throw err;
}
}
return await this.repository.updateFull(id, data);
}


async delete(id) {
if (!id) {
const error = new Error(this.errors.missingParameter);
error.parameterName = 'id';
throw error;
}

const collectionIndex = await CollectionIndex.findOneAndRemove({ "collection_index.id": id });

return collectionIndex; // Note: collectionIndex is null if not found
return await this.repository.delete(id);
}


Expand All @@ -110,39 +50,11 @@ class CollectionIndexService {
* This is expected to be a remote URL that does not require authentication.
*/
async retrieveByUrl(url) {
try {
if (!url) {
const error = new Error(this.errors.missingParameter);
throw error;
}

const res = await superagent.get(url).set('Accept', 'application/json');

// Handle different error cases
if (res.notFound) {
const error = new Error(this.errors.notFound);
throw error;
} else if (res.badRequest) {
const error = new Error(this.errors.badRequest);
throw error;
}

// Parsing res.text handles both the content-type text/plain and application/json use cases
const collectionIndex = JSON.parse(res.text);
return collectionIndex;
} catch (err) {
if (err.code === 'ENOTFOUND') {
throw new Error(this.errors.hostNotFound);
} else if (err.code === 'ECONNREFUSED') {
throw new Error(this.errors.connectionRefused);
} else {
throw err;
}
}
return await this.repository.retrieveByUrl(url);
}

Check warning on line 54 in app/services/collection-indexes-service.js

View check run for this annotation

Codecov / codecov/patch

app/services/collection-indexes-service.js#L53-L54

Added lines #L53 - L54 were not covered by tests


async refresh(id) {
static async refresh(id) {
// Do nothing for now
await new Promise(resolve => process.nextTick(resolve));
return {};

Check warning on line 60 in app/services/collection-indexes-service.js

View check run for this annotation

Codecov / codecov/patch

app/services/collection-indexes-service.js#L58-L60

Added lines #L58 - L60 were not covered by tests
Expand Down

0 comments on commit aad6dd4

Please sign in to comment.