-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
generic explirer removal jjob (#398)
* generic explirer removal jjob * fix bug * fix tests --------- Co-authored-by: Antoine de Chevigné <antoine@tryethernal.com>
- Loading branch information
Showing
8 changed files
with
90 additions
and
44 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
const { Op } = require('sequelize'); | ||
const { Explorer, StripeSubscription, StripePlan, Workspace } = require('../models'); | ||
const { enqueue } = require('../lib/queue'); | ||
|
||
module.exports = async () => { | ||
const explorers = (await Explorer.findAll({ | ||
include: [ | ||
{ | ||
model: StripeSubscription, | ||
as: 'stripeSubscription', | ||
include: { | ||
model: StripePlan, | ||
as: 'stripePlan', | ||
where: { | ||
capabilities: { | ||
expiresAfter: { | ||
[Op.not]: null | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
{ | ||
model: Workspace, | ||
as: 'workspace' | ||
} | ||
] | ||
})).filter(e => !!e.stripeSubscription); | ||
|
||
const deleted = []; | ||
for (let i = 0; i < explorers.length; i++) { | ||
const explorer = explorers[i]; | ||
const expiresAfterDays = explorer.stripeSubscription.stripePlan.capabilities.expiresAfter; | ||
const expirationDate = new Date(explorer.createdAt); | ||
expirationDate.setDate(expirationDate.getDate() + expiresAfterDays); | ||
|
||
const daysDiff = Math.floor((expirationDate - new Date()) / (1000 * 60 * 60 * 24)); | ||
|
||
if (daysDiff <= 0) { | ||
await explorer.workspace.update({ pendingDeletion: true, public: false }); | ||
await explorer.safeDelete({ deleteSubscription: true }); | ||
await enqueue('workspaceReset', `workspaceReset-${explorer.workspaceId}`, { | ||
workspaceId: explorer.workspaceId, | ||
from: new Date(0), | ||
to: new Date() | ||
}); | ||
await enqueue('deleteWorkspace', `deleteWorkspace-${explorer.workspaceId}`, { workspaceId: explorer.workspaceId }); | ||
deleted.push(explorer.slug); | ||
} | ||
} | ||
return deleted; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters