Skip to content
This repository has been archived by the owner on Aug 21, 2024. It is now read-only.

Commit

Permalink
IR-1830 - Add project id to location and instance table (#10123)
Browse files Browse the repository at this point in the history
* Add projectId column to instance and location schemas

* Add projectId to LocationDrawer, AddEditLocationModal, LocationService, createLocations, match-instance.test, and location.test

* Refactor instance service calls to include projectId

* Drop foreign key and column 'projectId' in instance table

* fix locationId undefined error

* Remove projectId field from service calls

* formatting issues

* Add projectId field to LocationSeed and populate it in locationDataResolver

* Fix projectId population in instance resolvers

* Fix error in populating projectId into instance and improve error message

* Add projectId population in instance and location data

* Refactor optional projectId field in instance and location schemas

* Reverted changes to make projectId optional

* Moved projectId population to resolvers

* chore: Refactor locationDataResolver to improve error handling

---------

Co-authored-by: Hanzla Mateen <[email protected]>
  • Loading branch information
hurairahmateen and hanzlamateen authored May 29, 2024
1 parent ccf0915 commit 3036b1b
Show file tree
Hide file tree
Showing 8 changed files with 168 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export const LocationSeed: LocationType = {
slugifiedName: '',
maxUsersPerInstance: 10,
sceneId: '',
projectId: '',
sceneAsset: {} as any,
isLobby: false,
isFeatured: false,
Expand Down
4 changes: 4 additions & 0 deletions packages/common/src/schemas/networking/instance.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ export const instanceSchema = Type.Object(
format: 'uuid'
})
),
projectId: Type.String({
format: 'uuid'
}),
podName: Type.Optional(Type.String()),
currentUsers: Type.Integer(),
ended: Type.Optional(Type.Boolean()),
Expand Down Expand Up @@ -93,6 +96,7 @@ export const instanceQueryProperties = Type.Pick(instanceSchema, [
'roomCode',
'ipAddress',
'channelId',
'projectId',
'podName',
'currentUsers',
'ended',
Expand Down
4 changes: 4 additions & 0 deletions packages/common/src/schemas/social/location.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ export const locationSchema = Type.Object(
}),
name: Type.String(),
sceneId: Type.String(),
projectId: Type.String({
format: 'uuid'
}),
slugifiedName: Type.String(),
/** @todo review */
isLobby: Type.Boolean(),
Expand Down Expand Up @@ -94,6 +97,7 @@ export const locationQueryProperties = Type.Pick(locationSchema, [
'id',
'name',
'sceneId',
'projectId',
'slugifiedName',
'isLobby',
'isFeatured',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,17 @@ Ethereal Engine. All Rights Reserved.
import { resolve, virtual } from '@feathersjs/schema'
import { v4 as uuidv4 } from 'uuid'

import { InstanceID, InstanceQuery, InstanceType } from '@etherealengine/common/src/schemas/networking/instance.schema'
import {
InstanceID,
InstanceQuery,
InstanceType,
instancePath
} from '@etherealengine/common/src/schemas/networking/instance.schema'
import { channelPath } from '@etherealengine/common/src/schemas/social/channel.schema'
import { locationPath } from '@etherealengine/common/src/schemas/social/location.schema'
import { fromDateTimeSql, getDateTimeSql } from '@etherealengine/common/src/utils/datetime-sql'
import type { HookContext } from '@etherealengine/server-core/declarations'
import { BadRequest } from '@feathersjs/errors'

export const instanceResolver = resolve<InstanceType, HookContext>({
location: virtual(async (instance, context) => {
Expand All @@ -48,6 +55,30 @@ export const instanceDataResolver = resolve<InstanceType, HookContext>({
id: async () => {
return uuidv4() as InstanceID
},
projectId: async (value, instance, context) => {
try {
// Populate projectId from locationId
if (instance.locationId) {
const locationData = await context.app.service(locationPath).get(instance.locationId)
if (locationData) {
return locationData.projectId
} else {
throw new BadRequest('Error populating projectId into world instance')
}
}
// Populate projectId from channelId
if (instance.channelId) {
const channelData = await context.app.service(channelPath).get(instance.channelId)
if (channelData.instanceId) {
const channelInstance = await context.app.service(instancePath).get(channelData.instanceId)
return channelInstance.projectId
}
return ''
}
} catch (error) {
throw new BadRequest('Error populating projectId into instance')
}
},
createdAt: getDateTimeSql,
updatedAt: getDateTimeSql
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ describe('instance.test', () => {
testInstance = {
id: '' as InstanceID,
locationId: testLocation.id as LocationID,
projectId: testLocation.projectId,
roomCode: '' as RoomCode,
currentUsers: 0,
ended: false,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
CPAL-1.0 License
The contents of this file are subject to the Common Public Attribution License
Version 1.0. (the "License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
https://github.com/EtherealEngine/etherealengine/blob/dev/LICENSE.
The License is based on the Mozilla Public License Version 1.1, but Sections 14
and 15 have been added to cover use of software over a computer network and
provide for limited attribution for the Original Developer. In addition,
Exhibit A has been modified to be consistent with Exhibit B.
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the
specific language governing rights and limitations under the License.
The Original Code is Ethereal Engine.
The Original Developer is the Initial Developer. The Initial Developer of the
Original Code is the Ethereal Engine team.
All portions of the code written by the Ethereal Engine team are Copyright © 2021-2023
Ethereal Engine. All Rights Reserved.
*/

import { instancePath } from '@etherealengine/common/src/schemas/networking/instance.schema'
import type { Knex } from 'knex'

/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
export async function up(knex: Knex): Promise<void> {
const projectIdColumnExists = await knex.schema.hasColumn(instancePath, 'projectId')
if (!projectIdColumnExists) {
await knex.schema.alterTable(instancePath, async (table) => {
//@ts-ignore
table.uuid('projectId', 36).collate('utf8mb4_bin').nullable().index()

table.foreign('projectId').references('id').inTable('project').onDelete('CASCADE').onUpdate('CASCADE')
})
}
}

/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
export async function down(knex: Knex): Promise<void> {
const projectIdColumnExists = await knex.schema.hasColumn(instancePath, 'projectId')

if (projectIdColumnExists) {
await knex.schema.alterTable(instancePath, async (table) => {
table.dropForeign('projectId')
table.dropColumn('projectId')
})
}
}
10 changes: 10 additions & 0 deletions packages/server-core/src/social/location/location.resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ import { LocationID, LocationQuery, LocationType } from '@etherealengine/common/
import { UserID } from '@etherealengine/common/src/schemas/user/user.schema'
import { fromDateTimeSql, getDateTimeSql } from '@etherealengine/common/src/utils/datetime-sql'
import type { HookContext } from '@etherealengine/server-core/declarations'
import { BadRequest } from '@feathersjs/errors'
import slugify from 'slugify'
import { LocationService } from './location.class'

export const locationResolver = resolve<LocationType, HookContext>({
locationSetting: virtual(async (location, context) => {
Expand Down Expand Up @@ -86,6 +88,14 @@ export const locationDataResolver = resolve<LocationType, HookContext>({
slugifiedName: async (value, location) => {
if (location.name) return slugify(location.name, { lower: true })
},
projectId: async (value, location, context: HookContext<LocationService>) => {
try {
const asset = await context.app.service(assetPath).get(location.sceneId)
return asset.projectId
} catch (error) {
throw new BadRequest('Error populating projectId into location')
}
},
locationSetting: async (value, location) => {
return {
...location.locationSetting,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
CPAL-1.0 License
The contents of this file are subject to the Common Public Attribution License
Version 1.0. (the "License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
https://github.com/EtherealEngine/etherealengine/blob/dev/LICENSE.
The License is based on the Mozilla Public License Version 1.1, but Sections 14
and 15 have been added to cover use of software over a computer network and
provide for limited attribution for the Original Developer. In addition,
Exhibit A has been modified to be consistent with Exhibit B.
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the
specific language governing rights and limitations under the License.
The Original Code is Ethereal Engine.
The Original Developer is the Initial Developer. The Initial Developer of the
Original Code is the Ethereal Engine team.
All portions of the code written by the Ethereal Engine team are Copyright © 2021-2023
Ethereal Engine. All Rights Reserved.
*/

import { locationPath } from '@etherealengine/common/src/schemas/social/location.schema'
import type { Knex } from 'knex'

/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
export async function up(knex: Knex): Promise<void> {
const projectIdColumnExists = await knex.schema.hasColumn(locationPath, 'projectId')
if (!projectIdColumnExists) {
await knex.schema.alterTable(locationPath, async (table) => {
//@ts-ignore
table.uuid('projectId', 36).collate('utf8mb4_bin').nullable().index()

table.foreign('projectId').references('id').inTable('project').onDelete('CASCADE').onUpdate('CASCADE')
})
}
}

/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
export async function down(knex: Knex): Promise<void> {
const projectIdColumnExists = await knex.schema.hasColumn(locationPath, 'projectId')

if (projectIdColumnExists) {
await knex.schema.alterTable(locationPath, async (table) => {
table.dropForeign('projectId')
table.dropColumn('projectId')
})
}
}

0 comments on commit 3036b1b

Please sign in to comment.