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

Fix reset #329

Merged
merged 7 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions run/lib/firebase.js
Original file line number Diff line number Diff line change
Expand Up @@ -1501,12 +1501,12 @@ const updateWorkspaceSettings = async (userId, workspace, settings) => {
return newWorkspace.toJSON();
};

const resetWorkspace = async (userId, workspace, hourInterval) => {
const resetWorkspace = async (userId, workspace, dayInterval) => {
if (!userId || !String(workspace)) throw new Error('Missing parameter.');

const user = await User.findByAuthIdWithWorkspace(userId, String(workspace));
if (user && user.workspaces.length)
await user.workspaces[0].reset(hourInterval);
await user.workspaces[0].reset(dayInterval);
};

const getUserbyStripeCustomerId = async (stripeCustomerId) => {
Expand Down
53 changes: 34 additions & 19 deletions run/models/workspace.js
Original file line number Diff line number Diff line change
Expand Up @@ -1026,34 +1026,49 @@ module.exports = (sequelize, DataTypes) => {
});
}

safeDestroyIntegrityCheck() {
return sequelize.models.IntegrityCheck.destroy({ where: { workspaceId: this.id }});
async safeDestroyIntegrityCheck(transaction) {
const integrityCheck = await this.getIntegrityCheck();
if (integrityCheck)
await integrityCheck.destroy(transaction);

return this.update({ integrityCheckStartBlockNumber: null }, { transaction });
}

async safeDestroyRpcHealthCheck(transaction) {
const rpcHealthCheck = await this.getRpcHealthCheck();
if (rpcHealthCheck)
return rpcHealthCheck.destroy({ transaction });
}

safeDestroyAccounts() {
return sequelize.models.Account.destroy({ where: { workspaceId: this.id }});
async safeDestroyAccounts() {
return sequelize.transaction(async transaction => {
const accounts = await sequelize.models.Account.findAll();
for (let i = 0; i < accounts.length; i++)
await accounts[i].safeDestroy(transaction);
});
}

async reset(dayInterval, transaction) {
const filter = { where: { workspaceId: this.id }};
if (dayInterval)
filter['where']['createdAt'] = { [Op.lt]: sequelize.literal(`NOW() - interval '${dayInterval} day'`)};

const destroyAll = async (transaction) => {
await sequelize.models.IntegrityCheck.destroy(filter, { transaction });
await sequelize.models.RpcHealthCheck.destroy(filter, { transaction });
await sequelize.models.TokenBalanceChange.destroy(filter, { transaction });
await sequelize.models.TokenTransfer.destroy(filter, { transaction });
await sequelize.models.ContractSource.destroy(filter, { transaction });
await sequelize.models.ContractVerification.destroy(filter, { transaction });
await sequelize.models.Erc721Token.destroy(filter, { transaction });
await sequelize.models.Block.destroy(filter, { transaction });
await sequelize.models.Transaction.destroy(filter, { transaction });
await sequelize.models.TransactionTraceStep.destroy(filter, { transaction });
await sequelize.models.TransactionReceipt.destroy(filter, { transaction });
await sequelize.models.TransactionLog.destroy(filter, { transaction });
await sequelize.models.Contract.destroy(filter, { transaction });
await sequelize.models.Account.destroy(filter, { transaction });
const destroyAll = async transaction => {
await this.safeDestroyIntegrityCheck(transaction);
await this.safeDestroyRpcHealthCheck(transaction);

const blocks = await sequelize.models.Block.findAll(filter);
for (let i = 0; i < blocks.length; i++)
await blocks[i].safeDestroy(transaction);

const contracts = await sequelize.models.Contract.findAll(filter);
for (let i = 0; i < contracts.length; i++)
await contracts[i].safeDestroy(transaction);

const accounts = await sequelize.models.Account.findAll(filter);
for (let i = 0; i < accounts.length; i++)
await accounts[i].safeDestroy(transaction);

};

return transaction ?
Expand Down
Loading