-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
mirror-web-server updates
- Loading branch information
Showing
90 changed files
with
6,217 additions
and
3,104 deletions.
There are no files selected for viewing
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
111 changes: 111 additions & 0 deletions
111
mirror-web-server/migrations/themirror/20240313102605-add-role-for-script-entity.js
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,111 @@ | ||
const mongodb = require('mongodb') | ||
const MIGRATION_SCRIPT_NAME = '20240313102605-add-role-for-script-entity' | ||
module.exports = { | ||
async up(db, client) { | ||
const spacesCollection = db.collection('spaces') | ||
const spacesObjectsCollection = db.collection('spaceobjects') | ||
const scriptEntityCollection = db.collection('scriptentities') | ||
const rolesCollection = db.collection('roles') | ||
const migrationScriptKey = `migrationScript.${MIGRATION_SCRIPT_NAME}` | ||
const scripts = await scriptEntityCollection.find({}).toArray() | ||
const bulkOps = [] | ||
const rolesInsertData = [] | ||
for (const script of scripts) { | ||
const owner = await getScriptOwner(script._id) | ||
|
||
if (owner) { | ||
const roleId = new mongodb.ObjectId() | ||
const newRole = { | ||
_id: roleId, | ||
defaultRole: 100, //ROLE.OBSERVER | ||
users: { | ||
[owner.toString()]: 1000 | ||
}, | ||
userGroups: null, | ||
createdAt: new Date(), | ||
updatedAt: new Date(), | ||
creator: new mongodb.ObjectId(owner.toString()), | ||
migratedViaScriptAt: new Date(), | ||
migrationScript: { | ||
[MIGRATION_SCRIPT_NAME]: true | ||
} | ||
} | ||
|
||
// save the role data to insert later | ||
rolesInsertData.push(newRole) | ||
|
||
bulkOps.push({ | ||
updateOne: { | ||
filter: { _id: script._id }, | ||
update: [ | ||
{ | ||
$set: { | ||
creator: new mongodb.ObjectId(owner.toString()), | ||
role: newRole, | ||
[migrationScriptKey]: true | ||
} | ||
} | ||
] | ||
} | ||
}) | ||
} | ||
} | ||
|
||
if (rolesInsertData.length > 0) { | ||
await rolesCollection.insertMany(rolesInsertData) | ||
} | ||
|
||
if (bulkOps.length > 0) { | ||
await scriptEntityCollection.bulkWrite(bulkOps) | ||
} | ||
|
||
async function getScriptOwner(scriptId) { | ||
// first check in spaces collection | ||
const space = await spacesCollection.findOne({ | ||
$or: [ | ||
{ scriptIds: scriptId.toString() }, | ||
{ 'scriptInstances.script_id': scriptId.toString() } | ||
] | ||
}) | ||
|
||
if (space && space.creator) { | ||
return space.creator | ||
} | ||
|
||
// then check in space objects collection | ||
const spaceObject = await spacesObjectsCollection.findOne({ | ||
$or: [ | ||
{ 'scriptEvents.script_id': scriptId.toString() }, | ||
{ 'scriptEvents.script_id': scriptId } | ||
] | ||
}) | ||
|
||
if (spaceObject && spaceObject.space) { | ||
const spaceFormSpaceObject = await spacesCollection.findOne({ | ||
_id: spaceObject.space | ||
}) | ||
|
||
if (spaceFormSpaceObject) { | ||
return spaceFormSpaceObject.creator | ||
} | ||
} | ||
|
||
return undefined | ||
} | ||
}, | ||
|
||
async down(db, client) { | ||
const scriptEntityCollection = db.collection('scriptentities') | ||
const rolesCollection = db.collection('roles') | ||
const migrationScriptKey = `migrationScript.${MIGRATION_SCRIPT_NAME}` | ||
|
||
await scriptEntityCollection.updateMany( | ||
{ [migrationScriptKey]: { $exists: true } }, | ||
[{ $unset: ['role', 'creator', migrationScriptKey] }] | ||
) | ||
|
||
await rolesCollection.deleteMany({ | ||
[migrationScriptKey]: { $exists: true } | ||
}) | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
...ver/migrations/themirror/20240321092745-add-creator-for-assets-where-they-do-not-exist.js
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,48 @@ | ||
const MIGRATION_SCRIPT_NAME = | ||
'20240321092745-add-creator-for-assets-where-they-do-not-exist' | ||
|
||
module.exports = { | ||
async up(db, client) { | ||
const assetsCollection = db.collection('assets') | ||
const migrationScriptKey = `migrationScript.${MIGRATION_SCRIPT_NAME}` | ||
|
||
const assetsWithoutCreator = await assetsCollection | ||
.find({ | ||
creator: { $exists: false }, | ||
owner: { $exists: true } | ||
}) | ||
.toArray() | ||
|
||
const bulkOps = [] | ||
|
||
for (const asset of assetsWithoutCreator) { | ||
bulkOps.push({ | ||
updateOne: { | ||
filter: { _id: asset._id }, | ||
update: [ | ||
{ | ||
$set: { | ||
creator: asset.owner, | ||
[migrationScriptKey]: true | ||
} | ||
} | ||
] | ||
} | ||
}) | ||
} | ||
|
||
if (bulkOps.length > 0) { | ||
await assetsCollection.bulkWrite(bulkOps) | ||
} | ||
}, | ||
|
||
async down(db, client) { | ||
const assetsCollection = db.collection('assets') | ||
const migrationScriptKey = `migrationScript.${MIGRATION_SCRIPT_NAME}` | ||
|
||
await assetsCollection.updateMany( | ||
{ [migrationScriptKey]: { $exists: true } }, | ||
[{ $unset: ['creator', migrationScriptKey] }] | ||
) | ||
} | ||
} |
143 changes: 143 additions & 0 deletions
143
mirror-web-server/migrations/themirror/20240401110610-add-products-for-purchaseOptions.js
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,143 @@ | ||
const { ObjectId } = require('mongodb') | ||
|
||
const { Stripe } = require('stripe') | ||
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY, { | ||
apiVersion: '2022-11-15' | ||
}) | ||
|
||
const MIGRATION_SCRIPT_NAME = '20240401110610-add-products-for-purchaseOptions' | ||
|
||
module.exports = { | ||
async up(db, client) { | ||
const assetsCollection = db.collection('assets') | ||
const migrationScriptKey = `migrationScript.${MIGRATION_SCRIPT_NAME}` | ||
|
||
// Get all assets with purchaseOptions | ||
const assets = await assetsCollection | ||
.find({ purchaseOptions: { $exists: 1 } }) | ||
.toArray() | ||
const bulkOps = [] | ||
assets.forEach(async (asset) => { | ||
for (let i = 0; i < asset.purchaseOptions.length; i++) { | ||
const purchaseOption = asset.purchaseOptions[i] | ||
|
||
// Check if the purchaseOption has an _id and is enabled | ||
if ( | ||
purchaseOption._id && | ||
purchaseOption.enabled && | ||
purchaseOption.type === 'ONE_TIME' | ||
) { | ||
try { | ||
// Create a product and price in Stripe | ||
await stripe.products.create({ | ||
id: purchaseOption._id.toString(), | ||
name: asset.name, | ||
description: | ||
asset.description === '' ? asset.name : asset.description, | ||
default_price_data: { | ||
currency: 'usd', | ||
unit_amount: Number(purchaseOption.price) * 100 | ||
} | ||
}) | ||
|
||
bulkOps.push({ | ||
updateOne: { | ||
filter: { _id: asset._id }, | ||
update: { | ||
$set: { | ||
[migrationScriptKey]: true | ||
} | ||
} | ||
} | ||
}) | ||
} catch (error) { | ||
console.log(error) | ||
} | ||
} else { | ||
const key = `purchaseOptions.$[${i}]` | ||
// Check if the purchaseOption has a price and is a ONE_TIME type | ||
if ( | ||
!purchaseOption._id && | ||
purchaseOption.price && | ||
purchaseOption.type === 'ONE_TIME' | ||
) { | ||
// Create a new ObjectId for the product | ||
const newId = new ObjectId() | ||
|
||
try { | ||
// Create a product and price in Stripe | ||
await stripe.products.create({ | ||
id: newId.toString(), | ||
name: asset.name, | ||
description: | ||
asset.description === '' ? asset.name : asset.description, | ||
default_price_data: { | ||
currency: 'usd', | ||
unit_amount: Number(purchaseOption.price) * 100 | ||
} | ||
}) | ||
|
||
bulkOps.push({ | ||
updateOne: { | ||
filter: { _id: asset._id }, | ||
update: { | ||
$set: { | ||
[migrationScriptKey]: true, | ||
[`${key}._id`]: newId | ||
} | ||
} | ||
} | ||
}) | ||
} catch (error) { | ||
console.log(error) | ||
} | ||
} | ||
} | ||
} | ||
}) | ||
if (bulkOps.length) { | ||
await assetsCollection.bulkWrite(bulkOps) | ||
} | ||
}, | ||
|
||
async down(db, client) { | ||
const assetsCollection = db.collection('assets') | ||
const migrationScriptKey = `migrationScript.${MIGRATION_SCRIPT_NAME}` | ||
|
||
const assets = await assetsCollection | ||
.find({ [migrationScriptKey]: true }) | ||
.toArray() | ||
|
||
const bulkOps = [] | ||
for (let j = 0; j < assets.length; j++) { | ||
const asset = assets[j] | ||
for (let i = 0; i < asset.purchaseOptions.length; i++) { | ||
const purchaseOption = asset.purchaseOptions[i] | ||
if ( | ||
purchaseOption._id && | ||
purchaseOption.enabled && | ||
purchaseOption.type === 'ONE_TIME' | ||
) { | ||
await stripe.products.update(purchaseOption._id.toString(), { | ||
active: false | ||
}) | ||
|
||
bulkOps.push({ | ||
updateOne: { | ||
filter: { _id: asset._id }, | ||
update: { | ||
$unset: { | ||
[migrationScriptKey]: true | ||
} | ||
} | ||
} | ||
}) | ||
} | ||
} | ||
} | ||
|
||
if (bulkOps.length) { | ||
await assetsCollection.bulkWrite(bulkOps) | ||
} | ||
} | ||
} |
Oops, something went wrong.