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

Changes for aws setting service to use schema, knex & feathers 5 #7907

Merged
merged 2 commits into from
Apr 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions packages/client-core/src/admin/components/Setting/Aws.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, { useEffect } from 'react'
import { useTranslation } from 'react-i18next'

import InputText from '@etherealengine/client-core/src/common/components/InputText'
import { AwsCloudFrontType, AwsSmsType } from '@etherealengine/engine/src/schemas/setting/aws-setting.schema'
import { getMutableState, useHookstate } from '@etherealengine/hyperflux'
import Box from '@etherealengine/ui/src/Box'
import Button from '@etherealengine/ui/src/Button'
Expand Down Expand Up @@ -38,8 +39,8 @@ const Aws = () => {

useEffect(() => {
if (awsSetting) {
let tempSms = JSON.parse(JSON.stringify(awsSetting?.sms))
let tempCloudfront = JSON.parse(JSON.stringify(awsSetting?.cloudfront))
const tempSms = JSON.parse(JSON.stringify(awsSetting?.sms)) as AwsSmsType
const tempCloudfront = JSON.parse(JSON.stringify(awsSetting?.cloudfront)) as AwsCloudFrontType
sms.set(tempSms)
cloudfront.set(tempCloudfront)
}
Expand All @@ -48,15 +49,12 @@ const Aws = () => {
const handleSubmit = (event) => {
event.preventDefault()

AwsSettingService.patchAwsSetting(
{ sms: JSON.stringify(sms.value), cloudfront: JSON.stringify(cloudfront.value) },
id
)
AwsSettingService.patchAwsSetting({ sms: sms.value, cloudfront: cloudfront.value }, id)
}

const handleCancel = () => {
let tempSms = JSON.parse(JSON.stringify(awsSetting?.sms))
let tempCloudfront = JSON.parse(JSON.stringify(awsSetting?.cloudfront))
const tempSms = JSON.parse(JSON.stringify(awsSetting?.sms)) as AwsSmsType
const tempCloudfront = JSON.parse(JSON.stringify(awsSetting?.cloudfront)) as AwsCloudFrontType
sms.set(tempSms)
cloudfront.set(tempCloudfront)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { Paginated } from '@feathersjs/feathers'

import { AdminAwsSetting, PatchAwsSetting } from '@etherealengine/common/src/interfaces/AdminAwsSetting'
import { matches, Validator } from '@etherealengine/engine/src/common/functions/MatchesUtils'
import {
AwsSettingPatch,
awsSettingPath,
AwsSettingType
} from '@etherealengine/engine/src/schemas/setting/aws-setting.schema'
import { defineAction, defineState, dispatchAction, getMutableState } from '@etherealengine/hyperflux'

import { API } from '../../../API'
Expand All @@ -10,7 +14,7 @@ import { NotificationService } from '../../../common/services/NotificationServic
export const AdminAwsSettingState = defineState({
name: 'AdminAwsSettingState',
initial: () => ({
awsSettings: [] as Array<AdminAwsSetting>,
awsSettings: [] as Array<AwsSettingType>,
skip: 0,
limit: 100,
total: 0,
Expand All @@ -36,15 +40,15 @@ export const AwsSettingReceptors = {
export const AwsSettingService = {
fetchAwsSetting: async () => {
try {
const awsSettings = (await API.instance.client.service('aws-setting').find()) as Paginated<AdminAwsSetting>
const awsSettings = (await API.instance.client.service(awsSettingPath).find()) as Paginated<AwsSettingType>
dispatchAction(AdminAwsSettingActions.awsSettingRetrieved({ awsSettings }))
} catch (err) {
NotificationService.dispatchNotify(err.message, { variant: 'error' })
}
},
patchAwsSetting: async (data: PatchAwsSetting, id: string) => {
patchAwsSetting: async (data: AwsSettingPatch, id: string) => {
try {
await API.instance.client.service('aws-setting').patch(id, data)
await API.instance.client.service(awsSettingPath).patch(id, data)
dispatchAction(AdminAwsSettingActions.awsSettingPatched({}))
} catch (err) {
NotificationService.dispatchNotify(err.message, { variant: 'error' })
Expand All @@ -55,7 +59,7 @@ export const AwsSettingService = {
export class AdminAwsSettingActions {
static awsSettingRetrieved = defineAction({
type: 'ee.client.AdminAwsSetting.ADMIN_AWS_SETTING_FETCHED' as const,
awsSettings: matches.object as Validator<unknown, Paginated<AdminAwsSetting>>
awsSettings: matches.object as Validator<unknown, Paginated<AwsSettingType>>
})
static awsSettingPatched = defineAction({
type: 'ee.client.AdminAwsSetting.ADMIN_AWS_SETTING_PATCHED' as const
Expand Down
8 changes: 0 additions & 8 deletions packages/common/src/dbmodels/Aws.ts

This file was deleted.

52 changes: 0 additions & 52 deletions packages/common/src/interfaces/AdminAwsSetting.ts

This file was deleted.

116 changes: 116 additions & 0 deletions packages/engine/src/schemas/setting/aws-setting.schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
// // For more information about this file see https://dove.feathersjs.com/guides/cli/service.schemas.html
import { querySyntax, Type } from '@feathersjs/typebox'
import type { Static } from '@feathersjs/typebox'

export const awsSettingPath = 'aws-setting'

export const awsSettingMethods = ['find', 'get', 'create', 'patch', 'remove'] as const

export const awsKeysSchema = Type.Object(
{
accessKeyId: Type.String(),
secretAccessKey: Type.String()
},
{ $id: 'AwsKeys', additionalProperties: false }
)
export type AwsKeysType = Static<typeof awsKeysSchema>

export const awsRoute53Schema = Type.Object(
{
hostedZoneId: Type.String(),
keys: Type.Ref(awsKeysSchema)
},
{ $id: 'AwsRoute53', additionalProperties: false }
)
export type AwsRoute53Type = Static<typeof awsRoute53Schema>

export const awsS3Schema = Type.Object(
{
baseUrl: Type.String(),
staticResourceBucket: Type.String(),
region: Type.String(),
avatarDir: Type.String(),
s3DevMode: Type.String()
},
{ $id: 'AwsS3', additionalProperties: false }
)
export type AwsS3Type = Static<typeof awsS3Schema>

export const awsCloudFrontSchema = Type.Object(
{
domain: Type.String(),
distributionId: Type.String(),
region: Type.String()
},
{ $id: 'AwsCloudFront', additionalProperties: false }
)
export type AwsCloudFrontType = Static<typeof awsCloudFrontSchema>

export const awsSmsSchema = Type.Object(
{
accessKeyId: Type.String(),
applicationId: Type.String(),
region: Type.String(),
senderId: Type.String(),
secretAccessKey: Type.String()
},
{ $id: 'AwsSms', additionalProperties: false }
)
export type AwsSmsType = Static<typeof awsSmsSchema>

// Main data model schema
export const awsSettingSchema = Type.Object(
{
id: Type.String({
format: 'uuid'
}),
keys: Type.Ref(awsKeysSchema),
route53: Type.Ref(awsRoute53Schema),
s3: Type.Ref(awsS3Schema),
cloudfront: Type.Ref(awsCloudFrontSchema),
sms: Type.Ref(awsSmsSchema),
createdAt: Type.String({ format: 'date-time' }),
updatedAt: Type.String({ format: 'date-time' })
},
{ $id: 'AwsSetting', additionalProperties: false }
)
export type AwsSettingType = Static<typeof awsSettingSchema>

export type AwsSettingDatabaseType = Omit<AwsSettingType, 'keys' | 'route53' | 's3' | 'cloudfront' | 'sms'> & {
keys: string
route53: string
s3: string
cloudfront: string
sms: string
}

// Schema for creating new entries
export const awsSettingDataSchema = Type.Pick(awsSettingSchema, ['keys', 'route53', 's3', 'cloudfront', 'sms'], {
$id: 'AwsSettingData'
})
export type AwsSettingData = Static<typeof awsSettingDataSchema>

// Schema for updating existing entries
export const awsSettingPatchSchema = Type.Partial(awsSettingSchema, {
$id: 'AwsSettingPatch'
})
export type AwsSettingPatch = Static<typeof awsSettingPatchSchema>

// Schema for allowed query properties
export const awsSettingQueryProperties = Type.Pick(awsSettingSchema, [
'id'
// 'keys', Commented out because: https://discord.com/channels/509848480760725514/1093914405546229840/1095101536121667694
// 'route53',
// 's3',
// 'cloudfront',
// 'sms'
])
export const awsSettingQuerySchema = Type.Intersect(
[
querySyntax(awsSettingQueryProperties),
// Add additional query properties here
Type.Object({}, { additionalProperties: false })
],
{ additionalProperties: false }
)
export type AwsSettingQuery = Static<typeof awsSettingQuerySchema>
76 changes: 19 additions & 57 deletions packages/server-core/src/setting/aws-setting/aws-setting.class.ts
Original file line number Diff line number Diff line change
@@ -1,57 +1,19 @@
import { NullableId, Paginated, Params } from '@feathersjs/feathers'
import { SequelizeServiceOptions, Service } from 'feathers-sequelize'

import { AdminAwsSetting as AdminAwsSettingInterface } from '@etherealengine/common/src/interfaces/AdminAwsSetting'

import { Application } from '../../../declarations'

export type AdminAwsSettingDataType = AdminAwsSettingInterface

export class Aws<T = AdminAwsSettingDataType> extends Service<T> {
app: Application

constructor(options: Partial<SequelizeServiceOptions>, app: Application) {
super(options)
this.app = app
}

async find(): Promise<T[] | Paginated<T>> {
const awsSetting = (await super.find()) as any
const data = awsSetting.data.map((el) => {
let keys = JSON.parse(el.keys)
let route53 = JSON.parse(el.route53)
let s3 = JSON.parse(el.s3)
let cloudfront = JSON.parse(el.cloudfront)
let sms = JSON.parse(el.sms)

if (typeof keys === 'string') keys = JSON.parse(keys)
if (typeof route53 === 'string') route53 = JSON.parse(route53)
if (typeof s3 === 'string') s3 = JSON.parse(s3)
if (typeof cloudfront === 'string') cloudfront = JSON.parse(cloudfront)
if (typeof sms === 'string') sms = JSON.parse(sms)

return {
...el,
keys: keys,
route53: {
...route53,
keys: JSON.parse(route53.keys)
},
s3: s3,
cloudfront: cloudfront,
sms: sms
}
})

return {
total: awsSetting.total,
limit: awsSetting.limit,
skip: awsSetting.skip,
data
}
}

patch(id: NullableId, data: any, params?: Params): Promise<T | T[]> {
return super.patch(id, data)
}
}
import type { Params } from '@feathersjs/feathers'
import { KnexService } from '@feathersjs/knex'
import type { KnexAdapterParams } from '@feathersjs/knex'

import {
AwsSettingData,
AwsSettingPatch,
AwsSettingQuery,
AwsSettingType
} from '@etherealengine/engine/src/schemas/setting/aws-setting.schema'

export interface AwsSettingParams extends KnexAdapterParams<AwsSettingQuery> {}

export class AwsSettingService<T = AwsSettingType, ServiceParams extends Params = AwsSettingParams> extends KnexService<
AwsSettingType,
AwsSettingData,
AwsSettingParams,
AwsSettingPatch
> {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { createSwaggerServiceOptions } from 'feathers-swagger'

import {
awsSettingDataSchema,
awsSettingPatchSchema,
awsSettingQuerySchema,
awsSettingSchema
} from '@etherealengine/engine/src/schemas/setting/aws-setting.schema'

export default createSwaggerServiceOptions({
schemas: {
awsSettingDataSchema,
awsSettingPatchSchema,
awsSettingQuerySchema,
awsSettingSchema
},
docs: {
description: 'Aws service description',
securities: ['all']
}
})
Loading