Skip to content

Commit

Permalink
feat: remove mirror
Browse files Browse the repository at this point in the history
  • Loading branch information
frantzarty committed Aug 22, 2022
1 parent 0455581 commit ece9fe4
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/controllers/organization.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
}
};
41 changes: 41 additions & 0 deletions src/datalayer/persistance.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 `,
Expand Down
5 changes: 5 additions & 0 deletions src/datalayer/writeService.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,15 @@ const dataLayerAvailable = async () => {
}
};

const removeMirror = (storeId, coinId) => {
return dataLayer.removeMirror(storeId, coinId);
};

export default {
dataLayerAvailable,
pushDataLayerChangeList,
syncDataLayer,
createDataLayerStore,
upsertDataLayer,
removeMirror,
};
4 changes: 4 additions & 0 deletions src/models/organizations/organizations.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand Down
9 changes: 9 additions & 0 deletions src/routes/v1/resources/organization.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
subscribeOrganizationSchema,
unsubscribeOrganizationSchema,
importHomeOrganizationSchema,
removeMirrorSchema,
} from '../../../validations';

const validator = joiExpress.createValidator({ passError: true });
Expand All @@ -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);
});
Expand Down
5 changes: 5 additions & 0 deletions src/validations/organizations.validations.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
});

0 comments on commit ece9fe4

Please sign in to comment.