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

Changes for email setting service to use schema, knex & feathers 5 #7883

Merged
merged 8 commits into from
Apr 17, 2023
25 changes: 14 additions & 11 deletions packages/client-core/src/admin/components/Setting/Email.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ const Email = () => {

useEffect(() => {
if (emailSetting) {
let tempSmtp = JSON.parse(JSON.stringify(emailSetting?.smtp))
let tempAuth = JSON.parse(JSON.stringify(emailSetting?.smtp?.auth))
let tempSubject = JSON.parse(JSON.stringify(emailSetting?.subject))
const tempSmtp = emailSetting?.smtp
const tempAuth = emailSetting?.smtp?.auth
const tempSubject = emailSetting?.subject

smtp.set(tempSmtp)
auth.set(tempAuth)
Expand All @@ -78,18 +78,18 @@ const Email = () => {

EmailSettingService.patchEmailSetting(
{
smtp: JSON.stringify({ ...smtp.value, auth: JSON.stringify(auth.value) }),
smtp: { ...smtp.value, auth: auth.value },
from: from.value,
subject: JSON.stringify(subject.value)
subject: subject.value
},
id
)
}

const handleCancel = () => {
let tempSmtp = JSON.parse(JSON.stringify(emailSetting?.smtp))
let tempAuth = JSON.parse(JSON.stringify(emailSetting?.smtp?.auth))
let tempSubject = JSON.parse(JSON.stringify(emailSetting?.subject))
const tempSmtp = emailSetting?.smtp
const tempAuth = emailSetting?.smtp?.auth
const tempSubject = emailSetting?.subject

smtp.set(tempSmtp)
auth.set(tempAuth)
Expand Down Expand Up @@ -141,12 +141,15 @@ const Email = () => {
name="pass"
label={t('admin:components.setting.password')}
value={auth?.value?.pass || ''}
type={showPassword ? 'text' : 'password'}
type={showPassword.value ? 'text' : 'password'}
endAdornment={
<IconButton
onClick={() => showPassword.set(!showPassword)}
onClick={() => showPassword.set(!showPassword.value)}
icon={
<Icon icon={showPassword ? 'ic:baseline-visibility' : 'ic:baseline-visibility-off'} color="orange" />
<Icon
icon={showPassword.value ? 'ic:baseline-visibility' : 'ic:baseline-visibility-off'}
color="orange"
/>
}
/>
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { Paginated } from '@feathersjs/feathers'

import { EmailSetting, PatchEmailSetting } from '@etherealengine/common/src/interfaces/EmailSetting'
import { matches, Validator } from '@etherealengine/engine/src/common/functions/MatchesUtils'
import {
EmailSettingPatch,
emailSettingPath,
EmailSettingType
} from '@etherealengine/engine/src/schemas/setting/email-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 AdminEmailSettingsState = defineState({
name: 'AdminEmailSettingsState',
initial: () => ({
email: [] as Array<EmailSetting>,
email: [] as Array<EmailSettingType>,
updateNeeded: true
})
})
Expand All @@ -33,16 +37,16 @@ export const EmailSettingReceptors = {
export const EmailSettingService = {
fetchedEmailSettings: async (inDec?: 'increment' | 'dcrement') => {
try {
const emailSettings = (await API.instance.client.service('email-setting').find()) as Paginated<EmailSetting>
const emailSettings = (await API.instance.client.service(emailSettingPath).find()) as Paginated<EmailSettingType>
dispatchAction(EmailSettingActions.fetchedEmail({ emailSettings }))
} catch (err) {
console.log(err.message)
NotificationService.dispatchNotify(err.message, { variant: 'error' })
}
},
patchEmailSetting: async (data: PatchEmailSetting, id: string) => {
patchEmailSetting: async (data: EmailSettingPatch, id: string) => {
try {
await API.instance.client.service('email-setting').patch(id, data)
await API.instance.client.service(emailSettingPath).patch(id, data)
dispatchAction(EmailSettingActions.emailSettingPatched({}))
} catch (err) {
NotificationService.dispatchNotify(err.message, { variant: 'error' })
Expand All @@ -53,7 +57,7 @@ export const EmailSettingService = {
export class EmailSettingActions {
static fetchedEmail = defineAction({
type: 'ee.client.EmailSetting.EMAIL_SETTING_DISPLAY' as const,
emailSettings: matches.object as Validator<unknown, Paginated<EmailSetting>>
emailSettings: matches.object as Validator<unknown, Paginated<EmailSettingType>>
})
static emailSettingPatched = defineAction({
type: 'ee.client.EmailSetting.EMAIL_SETTING_PATCHED' as const
Expand Down
7 changes: 0 additions & 7 deletions packages/common/src/dbmodels/EmailSetting.ts

This file was deleted.

32 changes: 0 additions & 32 deletions packages/common/src/interfaces/EmailSetting.ts

This file was deleted.

94 changes: 94 additions & 0 deletions packages/engine/src/schemas/setting/email-setting.schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// // 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 emailSettingPath = 'email-setting'

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

export const emailSubjectSchema = Type.Object(
{
'new-user': Type.String(),
location: Type.String(),
instance: Type.String(),
login: Type.String(),
friend: Type.String(),
group: Type.String(),
party: Type.String()
},
{ $id: 'EmailSubject', additionalProperties: false }
)
export type EmailSubjectType = Static<typeof emailSubjectSchema>

export const emailAuthSchema = Type.Object(
{
user: Type.String(),
pass: Type.String()
},
{ $id: 'EmailAuth', additionalProperties: false }
)
export type EmailAuthType = Static<typeof emailAuthSchema>

export const emailSmtpSchema = Type.Object(
{
host: Type.String(),
port: Type.String(),
secure: Type.Boolean(),
auth: Type.Ref(emailAuthSchema)
},
{ $id: 'EmailSmtp', additionalProperties: false }
)
export type EmailSmtpType = Static<typeof emailSmtpSchema>

// Main data model schema
export const emailSettingSchema = Type.Object(
{
id: Type.String({
format: 'uuid'
}),
smtp: Type.Ref(emailSmtpSchema),
from: Type.String(),
subject: Type.Ref(emailSubjectSchema),
smsNameCharacterLimit: Type.Number(),
createdAt: Type.String({ format: 'date-time' }),
updatedAt: Type.String({ format: 'date-time' })
},
{ $id: 'EmailSetting', additionalProperties: false }
)
export type EmailSettingType = Static<typeof emailSettingSchema>

export type EmailSettingDatabaseType = Omit<EmailSettingType, 'smtp' | 'subject'> & { smtp: string; subject: string }

// Schema for creating new entries
export const emailSettingDataSchema = Type.Pick(
emailSettingSchema,
['smtp', 'from', 'subject', 'smsNameCharacterLimit'],
{
$id: 'EmailSettingData'
}
)
export type EmailSettingData = Static<typeof emailSettingDataSchema>

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

// Schema for allowed query properties
export const emailSettingQueryProperties = Type.Pick(emailSettingSchema, [
'id',
// 'smtp', Commented out because: https://discord.com/channels/509848480760725514/1093914405546229840/1095101536121667694
'from',
// 'subject',
'smsNameCharacterLimit'
])
export const emailSettingQuerySchema = Type.Intersect(
[
querySyntax(emailSettingQueryProperties),
// Add additional query properties here
Type.Object({}, { additionalProperties: false })
],
{ additionalProperties: false }
)
export type EmailSettingQuery = Static<typeof emailSettingQuerySchema>
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { iff, isProvider } from 'feathers-hooks-common'
import {
analyticsDataSchema,
analyticsPatchSchema,
analyticsQuerySchema
analyticsQuerySchema,
analyticsSchema
} from '@etherealengine/engine/src/schemas/analytics/analytics.schema'
import { dataValidator, queryValidator } from '@etherealengine/server-core/validators'

Expand All @@ -19,6 +20,8 @@ import {
analyticsResolver
} from './analytics.resolvers'

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const analyticsValidator = getValidator(analyticsSchema, dataValidator)
const analyticsDataValidator = getValidator(analyticsDataSchema, dataValidator)
const analyticsPatchValidator = getValidator(analyticsPatchSchema, dataValidator)
const analyticsQueryValidator = getValidator(analyticsQuerySchema, queryValidator)
Expand All @@ -31,7 +34,7 @@ export default {
before: {
all: [
authenticate(),
iff(isProvider('external'), verifyScope('admin', 'admin') as any),
iff(isProvider('external'), verifyScope('admin', 'admin')),
schemaHooks.validateQuery(analyticsQueryValidator),
schemaHooks.resolveQuery(analyticsQueryResolver)
],
Expand Down Expand Up @@ -62,4 +65,4 @@ export default {
patch: [],
remove: []
}
} as any
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ export default (app: Application): void => {
multi: true
}

//@ts-ignore
app.use(analyticsPath, new AnalyticsService(options, app), {
// A list of all methods this service exposes externally
methods: analyticsMethods,
Expand Down
15 changes: 9 additions & 6 deletions packages/server-core/src/route/route/route.hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { iff, isProvider } from 'feathers-hooks-common'
import {
routeDataSchema,
routePatchSchema,
routeQuerySchema
routeQuerySchema,
routeSchema
} from '@etherealengine/engine/src/schemas/route/route.schema'
import { dataValidator, queryValidator } from '@etherealengine/server-core/validators'

Expand All @@ -19,6 +20,8 @@ import {
routeResolver
} from './route.resolvers'

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const routeValidator = getValidator(routeSchema, dataValidator)
const routeDataValidator = getValidator(routeDataSchema, dataValidator)
const routePatchValidator = getValidator(routePatchSchema, dataValidator)
const routeQueryValidator = getValidator(routeQuerySchema, queryValidator)
Expand All @@ -34,18 +37,18 @@ export default {
get: [],
create: [
authenticate(),
iff(isProvider('external'), verifyScope('admin', 'admin') as any),
iff(isProvider('external'), verifyScope('admin', 'admin')),
schemaHooks.validateData(routeDataValidator),
schemaHooks.resolveData(routeDataResolver)
],
update: [authenticate(), iff(isProvider('external'), verifyScope('admin', 'admin') as any)],
update: [authenticate(), iff(isProvider('external'), verifyScope('admin', 'admin'))],
patch: [
authenticate(),
iff(isProvider('external'), verifyScope('admin', 'admin') as any),
iff(isProvider('external'), verifyScope('admin', 'admin')),
schemaHooks.validateData(routePatchValidator),
schemaHooks.resolveData(routePatchResolver)
],
remove: [authenticate(), iff(isProvider('external'), verifyScope('admin', 'admin') as any)]
remove: [authenticate(), iff(isProvider('external'), verifyScope('admin', 'admin'))]
},

after: {
Expand All @@ -67,4 +70,4 @@ export default {
patch: [],
remove: []
}
} as any
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { iff, isProvider } from 'feathers-hooks-common'
import {
chargebeeSettingDataSchema,
chargebeeSettingPatchSchema,
chargebeeSettingQuerySchema
chargebeeSettingQuerySchema,
chargebeeSettingSchema
} from '@etherealengine/engine/src/schemas/setting/chargebee-setting.schema'
import { dataValidator, queryValidator } from '@etherealengine/server-core/validators'

Expand All @@ -19,6 +20,8 @@ import {
chargebeeSettingResolver
} from './chargebee-setting.resolvers'

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const chargebeeSettingValidator = getValidator(chargebeeSettingSchema, dataValidator)
const chargebeeSettingDataValidator = getValidator(chargebeeSettingDataSchema, dataValidator)
const chargebeeSettingPatchValidator = getValidator(chargebeeSettingPatchSchema, dataValidator)
const chargebeeSettingQueryValidator = getValidator(chargebeeSettingQuerySchema, queryValidator)
Expand All @@ -34,24 +37,24 @@ export default {
before: {
all: [
authenticate(),
iff(isProvider('external'), verifyScope('admin', 'admin') as any),
iff(isProvider('external'), verifyScope('admin', 'admin')),
schemaHooks.validateQuery(chargebeeSettingQueryValidator),
schemaHooks.resolveQuery(chargebeeSettingQueryResolver)
],
find: [iff(isProvider('external'), verifyScope('settings', 'read') as any)],
get: [iff(isProvider('external'), verifyScope('settings', 'read') as any)],
find: [iff(isProvider('external'), verifyScope('settings', 'read'))],
get: [iff(isProvider('external'), verifyScope('settings', 'read'))],
create: [
iff(isProvider('external'), verifyScope('settings', 'write') as any),
iff(isProvider('external'), verifyScope('settings', 'write')),
schemaHooks.validateData(chargebeeSettingDataValidator),
schemaHooks.resolveData(chargebeeSettingDataResolver)
],
update: [iff(isProvider('external'), verifyScope('settings', 'write') as any)],
update: [iff(isProvider('external'), verifyScope('settings', 'write'))],
patch: [
iff(isProvider('external'), verifyScope('settings', 'write') as any),
iff(isProvider('external'), verifyScope('settings', 'write')),
schemaHooks.validateData(chargebeeSettingPatchValidator),
schemaHooks.resolveData(chargebeeSettingPatchResolver)
],
remove: [iff(isProvider('external'), verifyScope('settings', 'write') as any)]
remove: [iff(isProvider('external'), verifyScope('settings', 'write'))]
},

after: {
Expand All @@ -73,4 +76,4 @@ export default {
patch: [],
remove: []
}
} as any
}
Loading