Skip to content

Commit

Permalink
feat:update code (#222)
Browse files Browse the repository at this point in the history
mirror-web-server updates
  • Loading branch information
Rybasher authored Jul 25, 2024
1 parent 25d61f5 commit 0688a54
Show file tree
Hide file tree
Showing 90 changed files with 6,217 additions and 3,104 deletions.
6 changes: 4 additions & 2 deletions mirror-web-server/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ POST_HOG_PERSONAL_API_KEY=
POST_HOG_PROJECT_ID=
POST_HOG_PROJECT_API_KEY=

# Mixpanel; it's replacing PostHog
MIXPANEL_TOKEN=

STRIPE_SECRET_KEY=
RETURN_STRIPE_URL=

Expand All @@ -62,5 +65,4 @@ DISABLE_EMAIL=true
ASSET_STORAGE_DRIVER=LOCAL

# if ASSET_STORAGE_DRIVER is not GCP, then this is required
ASSET_STORAGE_URL=http://localhost:9000/assets-storage/

ASSET_STORAGE_URL=http://localhost:9000/assets-storage/
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 }
})
}
}
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] }]
)
}
}
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)
}
}
}
Loading

0 comments on commit 0688a54

Please sign in to comment.