diff --git a/src/controllers/organization.controller.js b/src/controllers/organization.controller.js index 53c39fbe..2667559a 100644 --- a/src/controllers/organization.controller.js +++ b/src/controllers/organization.controller.js @@ -323,3 +323,21 @@ export const resyncOrganization = async (req, res) => { } } }; + +export const removeMirror = async (req, res) => { + try { + await assertIfReadOnlyMode(); + await assertWalletIsSynced(); + await assertHomeOrgExists(); + + await Organization.removeMirror(req.body.storeId, req.body.coinId); + return res.json({ + message: `Mirror removed for ${req.body.storeId}.`, + }); + } catch (error) { + res.status(400).json({ + message: 'Error removing mirror for organization', + error: error.message, + }); + } +}; diff --git a/src/datalayer/persistance.js b/src/datalayer/persistance.js index 9f8f844a..ae50e4ce 100644 --- a/src/datalayer/persistance.js +++ b/src/datalayer/persistance.js @@ -370,6 +370,47 @@ const _addMirror = async (storeId, url) => { } }; +export const removeMirror = async (storeId, coinId) => { + const mirrors = await getMirrors(storeId); + + // Dont add the mirror if it already exists. + const mirrorExists = mirrors.find( + (mirror) => mirror.coin_id === coinId && mirror.launcher_id === storeId, + ); + + if (mirrorExists) { + logger.error( + `Mirror doesnt exist for: storeId: ${storeId}, coinId: ${coinId}`, + ); + return false; + } + + try { + const options = { + url: `${rpcUrl}/delete_mirror`, + body: JSON.stringify({ + id: coinId, + }), + }; + + const response = await request( + Object.assign({}, getBaseOptions(), options), + ); + + const data = JSON.parse(response); + + if (data.success) { + logger.info(`Removed mirror for ${storeId}`); + return true; + } + + logger.error(`Failed removing mirror for ${storeId}`); + return false; + } catch (error) { + return false; + } +}; + const getMirrors = async (storeId) => { const options = { url: `${rpcUrl}/get_mirrors `, diff --git a/src/datalayer/writeService.js b/src/datalayer/writeService.js index 050246b6..52c14a37 100644 --- a/src/datalayer/writeService.js +++ b/src/datalayer/writeService.js @@ -165,10 +165,15 @@ const dataLayerAvailable = async () => { } }; +const removeMirror = (storeId, coinId) => { + return dataLayer.removeMirror(storeId, coinId); +}; + export default { dataLayerAvailable, pushDataLayerChangeList, syncDataLayer, createDataLayerStore, upsertDataLayer, + removeMirror, }; diff --git a/src/models/organizations/organizations.model.js b/src/models/organizations/organizations.model.js index 456e8441..575afbd0 100644 --- a/src/models/organizations/organizations.model.js +++ b/src/models/organizations/organizations.model.js @@ -381,6 +381,10 @@ class Organization extends Model { await datalayer.upsertDataLayer(myOrganization.orgUid, payload); }; + + static removeMirror = async (storeId, coinId) => { + datalayer.removeMirror(storeId, coinId); + }; } Organization.init(ModelTypes, { diff --git a/src/routes/v1/resources/organization.js b/src/routes/v1/resources/organization.js index 45b835db..415ba712 100644 --- a/src/routes/v1/resources/organization.js +++ b/src/routes/v1/resources/organization.js @@ -11,6 +11,7 @@ import { subscribeOrganizationSchema, unsubscribeOrganizationSchema, importHomeOrganizationSchema, + removeMirrorSchema, } from '../../../validations'; const validator = joiExpress.createValidator({ passError: true }); @@ -20,6 +21,14 @@ OrganizationRouter.get('/', (req, res) => { return OrganizationController.findAll(req, res); }); +OrganizationRouter.get( + '/organizations', + validator.body(removeMirrorSchema), + (req, res) => { + return OrganizationController.removeMirror(req, res); + }, +); + OrganizationRouter.delete('/', (req, res) => { return OrganizationController.resetHomeOrg(req, res); }); diff --git a/src/validations/organizations.validations.js b/src/validations/organizations.validations.js index 1f06bd57..bb3b0e45 100644 --- a/src/validations/organizations.validations.js +++ b/src/validations/organizations.validations.js @@ -26,3 +26,8 @@ export const subscribeOrganizationSchema = Joi.object({ export const resyncOrganizationSchema = Joi.object({ orgUid: Joi.string().required(), }); + +export const removeMirrorSchema = Joi.object({ + orgUid: Joi.string().required(), + storeId: Joi.string().required(), +});