Skip to content

Commit 0688a54

Browse files
authored
feat:update code (#222)
mirror-web-server updates
1 parent 25d61f5 commit 0688a54

File tree

90 files changed

+6217
-3104
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

90 files changed

+6217
-3104
lines changed

mirror-web-server/.env.example

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ POST_HOG_PERSONAL_API_KEY=
3636
POST_HOG_PROJECT_ID=
3737
POST_HOG_PROJECT_API_KEY=
3838

39+
# Mixpanel; it's replacing PostHog
40+
MIXPANEL_TOKEN=
41+
3942
STRIPE_SECRET_KEY=
4043
RETURN_STRIPE_URL=
4144

@@ -62,5 +65,4 @@ DISABLE_EMAIL=true
6265
ASSET_STORAGE_DRIVER=LOCAL
6366

6467
# if ASSET_STORAGE_DRIVER is not GCP, then this is required
65-
ASSET_STORAGE_URL=http://localhost:9000/assets-storage/
66-
68+
ASSET_STORAGE_URL=http://localhost:9000/assets-storage/
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
const mongodb = require('mongodb')
2+
const MIGRATION_SCRIPT_NAME = '20240313102605-add-role-for-script-entity'
3+
module.exports = {
4+
async up(db, client) {
5+
const spacesCollection = db.collection('spaces')
6+
const spacesObjectsCollection = db.collection('spaceobjects')
7+
const scriptEntityCollection = db.collection('scriptentities')
8+
const rolesCollection = db.collection('roles')
9+
const migrationScriptKey = `migrationScript.${MIGRATION_SCRIPT_NAME}`
10+
const scripts = await scriptEntityCollection.find({}).toArray()
11+
const bulkOps = []
12+
const rolesInsertData = []
13+
for (const script of scripts) {
14+
const owner = await getScriptOwner(script._id)
15+
16+
if (owner) {
17+
const roleId = new mongodb.ObjectId()
18+
const newRole = {
19+
_id: roleId,
20+
defaultRole: 100, //ROLE.OBSERVER
21+
users: {
22+
[owner.toString()]: 1000
23+
},
24+
userGroups: null,
25+
createdAt: new Date(),
26+
updatedAt: new Date(),
27+
creator: new mongodb.ObjectId(owner.toString()),
28+
migratedViaScriptAt: new Date(),
29+
migrationScript: {
30+
[MIGRATION_SCRIPT_NAME]: true
31+
}
32+
}
33+
34+
// save the role data to insert later
35+
rolesInsertData.push(newRole)
36+
37+
bulkOps.push({
38+
updateOne: {
39+
filter: { _id: script._id },
40+
update: [
41+
{
42+
$set: {
43+
creator: new mongodb.ObjectId(owner.toString()),
44+
role: newRole,
45+
[migrationScriptKey]: true
46+
}
47+
}
48+
]
49+
}
50+
})
51+
}
52+
}
53+
54+
if (rolesInsertData.length > 0) {
55+
await rolesCollection.insertMany(rolesInsertData)
56+
}
57+
58+
if (bulkOps.length > 0) {
59+
await scriptEntityCollection.bulkWrite(bulkOps)
60+
}
61+
62+
async function getScriptOwner(scriptId) {
63+
// first check in spaces collection
64+
const space = await spacesCollection.findOne({
65+
$or: [
66+
{ scriptIds: scriptId.toString() },
67+
{ 'scriptInstances.script_id': scriptId.toString() }
68+
]
69+
})
70+
71+
if (space && space.creator) {
72+
return space.creator
73+
}
74+
75+
// then check in space objects collection
76+
const spaceObject = await spacesObjectsCollection.findOne({
77+
$or: [
78+
{ 'scriptEvents.script_id': scriptId.toString() },
79+
{ 'scriptEvents.script_id': scriptId }
80+
]
81+
})
82+
83+
if (spaceObject && spaceObject.space) {
84+
const spaceFormSpaceObject = await spacesCollection.findOne({
85+
_id: spaceObject.space
86+
})
87+
88+
if (spaceFormSpaceObject) {
89+
return spaceFormSpaceObject.creator
90+
}
91+
}
92+
93+
return undefined
94+
}
95+
},
96+
97+
async down(db, client) {
98+
const scriptEntityCollection = db.collection('scriptentities')
99+
const rolesCollection = db.collection('roles')
100+
const migrationScriptKey = `migrationScript.${MIGRATION_SCRIPT_NAME}`
101+
102+
await scriptEntityCollection.updateMany(
103+
{ [migrationScriptKey]: { $exists: true } },
104+
[{ $unset: ['role', 'creator', migrationScriptKey] }]
105+
)
106+
107+
await rolesCollection.deleteMany({
108+
[migrationScriptKey]: { $exists: true }
109+
})
110+
}
111+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
const MIGRATION_SCRIPT_NAME =
2+
'20240321092745-add-creator-for-assets-where-they-do-not-exist'
3+
4+
module.exports = {
5+
async up(db, client) {
6+
const assetsCollection = db.collection('assets')
7+
const migrationScriptKey = `migrationScript.${MIGRATION_SCRIPT_NAME}`
8+
9+
const assetsWithoutCreator = await assetsCollection
10+
.find({
11+
creator: { $exists: false },
12+
owner: { $exists: true }
13+
})
14+
.toArray()
15+
16+
const bulkOps = []
17+
18+
for (const asset of assetsWithoutCreator) {
19+
bulkOps.push({
20+
updateOne: {
21+
filter: { _id: asset._id },
22+
update: [
23+
{
24+
$set: {
25+
creator: asset.owner,
26+
[migrationScriptKey]: true
27+
}
28+
}
29+
]
30+
}
31+
})
32+
}
33+
34+
if (bulkOps.length > 0) {
35+
await assetsCollection.bulkWrite(bulkOps)
36+
}
37+
},
38+
39+
async down(db, client) {
40+
const assetsCollection = db.collection('assets')
41+
const migrationScriptKey = `migrationScript.${MIGRATION_SCRIPT_NAME}`
42+
43+
await assetsCollection.updateMany(
44+
{ [migrationScriptKey]: { $exists: true } },
45+
[{ $unset: ['creator', migrationScriptKey] }]
46+
)
47+
}
48+
}
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
const { ObjectId } = require('mongodb')
2+
3+
const { Stripe } = require('stripe')
4+
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY, {
5+
apiVersion: '2022-11-15'
6+
})
7+
8+
const MIGRATION_SCRIPT_NAME = '20240401110610-add-products-for-purchaseOptions'
9+
10+
module.exports = {
11+
async up(db, client) {
12+
const assetsCollection = db.collection('assets')
13+
const migrationScriptKey = `migrationScript.${MIGRATION_SCRIPT_NAME}`
14+
15+
// Get all assets with purchaseOptions
16+
const assets = await assetsCollection
17+
.find({ purchaseOptions: { $exists: 1 } })
18+
.toArray()
19+
const bulkOps = []
20+
assets.forEach(async (asset) => {
21+
for (let i = 0; i < asset.purchaseOptions.length; i++) {
22+
const purchaseOption = asset.purchaseOptions[i]
23+
24+
// Check if the purchaseOption has an _id and is enabled
25+
if (
26+
purchaseOption._id &&
27+
purchaseOption.enabled &&
28+
purchaseOption.type === 'ONE_TIME'
29+
) {
30+
try {
31+
// Create a product and price in Stripe
32+
await stripe.products.create({
33+
id: purchaseOption._id.toString(),
34+
name: asset.name,
35+
description:
36+
asset.description === '' ? asset.name : asset.description,
37+
default_price_data: {
38+
currency: 'usd',
39+
unit_amount: Number(purchaseOption.price) * 100
40+
}
41+
})
42+
43+
bulkOps.push({
44+
updateOne: {
45+
filter: { _id: asset._id },
46+
update: {
47+
$set: {
48+
[migrationScriptKey]: true
49+
}
50+
}
51+
}
52+
})
53+
} catch (error) {
54+
console.log(error)
55+
}
56+
} else {
57+
const key = `purchaseOptions.$[${i}]`
58+
// Check if the purchaseOption has a price and is a ONE_TIME type
59+
if (
60+
!purchaseOption._id &&
61+
purchaseOption.price &&
62+
purchaseOption.type === 'ONE_TIME'
63+
) {
64+
// Create a new ObjectId for the product
65+
const newId = new ObjectId()
66+
67+
try {
68+
// Create a product and price in Stripe
69+
await stripe.products.create({
70+
id: newId.toString(),
71+
name: asset.name,
72+
description:
73+
asset.description === '' ? asset.name : asset.description,
74+
default_price_data: {
75+
currency: 'usd',
76+
unit_amount: Number(purchaseOption.price) * 100
77+
}
78+
})
79+
80+
bulkOps.push({
81+
updateOne: {
82+
filter: { _id: asset._id },
83+
update: {
84+
$set: {
85+
[migrationScriptKey]: true,
86+
[`${key}._id`]: newId
87+
}
88+
}
89+
}
90+
})
91+
} catch (error) {
92+
console.log(error)
93+
}
94+
}
95+
}
96+
}
97+
})
98+
if (bulkOps.length) {
99+
await assetsCollection.bulkWrite(bulkOps)
100+
}
101+
},
102+
103+
async down(db, client) {
104+
const assetsCollection = db.collection('assets')
105+
const migrationScriptKey = `migrationScript.${MIGRATION_SCRIPT_NAME}`
106+
107+
const assets = await assetsCollection
108+
.find({ [migrationScriptKey]: true })
109+
.toArray()
110+
111+
const bulkOps = []
112+
for (let j = 0; j < assets.length; j++) {
113+
const asset = assets[j]
114+
for (let i = 0; i < asset.purchaseOptions.length; i++) {
115+
const purchaseOption = asset.purchaseOptions[i]
116+
if (
117+
purchaseOption._id &&
118+
purchaseOption.enabled &&
119+
purchaseOption.type === 'ONE_TIME'
120+
) {
121+
await stripe.products.update(purchaseOption._id.toString(), {
122+
active: false
123+
})
124+
125+
bulkOps.push({
126+
updateOne: {
127+
filter: { _id: asset._id },
128+
update: {
129+
$unset: {
130+
[migrationScriptKey]: true
131+
}
132+
}
133+
}
134+
})
135+
}
136+
}
137+
}
138+
139+
if (bulkOps.length) {
140+
await assetsCollection.bulkWrite(bulkOps)
141+
}
142+
}
143+
}

0 commit comments

Comments
 (0)