Improved upload

This commit is contained in:
Sebastian Seedorf
2022-08-18 10:38:48 +02:00
parent ffb93d9da4
commit d97786f880
11 changed files with 93 additions and 52 deletions

View File

@@ -16,16 +16,26 @@ export type InsertMeta<T> = T & {
type GroupFilter = Filter<InsertMeta<GroupData>>
export async function setGroups(data: GroupData): Promise<string | undefined> {
export async function setData(uuid: string|undefined, data: GroupData): Promise<string | undefined> {
const collection = (await database.resolve())?.collection('setups')
if (!collection) return
const result = await collection.insertOne({
...data,
createdOn: new Date(),
modifiedOn: new Date(),
accessedOn: new Date()
})
return result.insertedId.toString()
if (!uuid) {
const result = await collection.insertOne({
...data,
createdOn: new Date(),
modifiedOn: new Date(),
accessedOn: new Date()
})
return result.insertedId.toString()
} else {
await collection.findOneAndReplace(getUuid(uuid), {
...data,
createdOn: new Date(),
modifiedOn: new Date(),
accessedOn: new Date()
})
return uuid
}
}
function getUuid(uuid: string): GroupFilter {
@@ -45,7 +55,7 @@ export async function setFactories(
return true
}
export async function getGroup(uuid: string) {
export async function getData(uuid: string) {
const collection = (await database.resolve())?.collection('setups')
if (!collection) return
const data = (await collection.findOne(getUuid(uuid))) ?? undefined
@@ -62,7 +72,7 @@ export async function renameGroup(
): Promise<boolean> {
oldName = oldName.replace(/[.$]/g, '')
newName = newName.replace(/[.$]/g, '')
const data = await getGroup(uuid)
const data = await getData(uuid)
if (data?.groups && !(newName in data.groups)) {
const collection = (await database.resolve())?.collection('setups')
if (!collection) return false
@@ -83,7 +93,7 @@ export async function renameGroup(
export async function addGroup(uuid: string, name: string): Promise<boolean> {
name = name.replace(/[.$]/g, '')
const data = await getGroup(uuid)
const data = await getData(uuid)
if (data?.groups && !(name in data.groups)) {
const collection = (await database.resolve())?.collection('setups')
if (!collection) return false
@@ -97,7 +107,7 @@ export async function addGroup(uuid: string, name: string): Promise<boolean> {
export async function removeGroup(uuid: string, name: string): Promise<boolean> {
name = name.replace(/[.$]/g, '')
const data = await getGroup(uuid)
const data = await getData(uuid)
if (data?.groups && name in data.groups) {
const collection = (await database.resolve())?.collection('setups')
if (!collection) return false
@@ -114,7 +124,7 @@ export async function setFactoriesOfGroup(
factories: string[]
): Promise<boolean> {
name = name.replace(/[.$]/g, '')
const data = await getGroup(uuid)
const data = await getData(uuid)
if (data?.groups && name in data.groups) {
const collection = (await database.resolve())?.collection('setups')
if (!collection) return false

View File

@@ -1,5 +1,5 @@
import { GetServerSideProps } from 'next'
import { getGroup, setGroups } from './database/groups'
import { getData, setData } from './database/groups'
import { Dict, Group } from './types'
export interface PropsGroupProvider {
@@ -13,7 +13,7 @@ export const getServerSidePropsGroupProvider: GetServerSideProps<PropsGroupProvi
query
}) => {
const id = Array.isArray(query?.id) ? query.id[0] : query?.id
const data = id && (await getGroup(id))
const data = id?.match(/[a-f0-9]{24}/) && (await getData(id).catch(() => undefined))
if (data) {
return {
props: {
@@ -24,7 +24,7 @@ export const getServerSidePropsGroupProvider: GetServerSideProps<PropsGroupProvi
}
}
} else if (!id) {
const newId = await setGroups({ groups: {}, base: [], ignored: [] })
const newId = await setData(undefined, { groups: {}, base: [], ignored: [] })
return {
redirect: {
destination: `?id=${newId}`,

View File

@@ -20,6 +20,17 @@ export interface SetFactoryArrayBody {
type: 'ignored' | 'base'
factories: string[]
}
export interface UploadDataBody {
groups: {
[k: string]: {
name: string
exports: string[]
malls: string[]
}
}
ignored: string[]
base: string[]
}
export interface IdParam {
id: string
}

View File

@@ -7,24 +7,30 @@
export interface GroupRenameBody {
newName: string
[k: string]: unknown
}
export interface GroupSetFactoryArrayBody {
type: 'exports' | 'malls'
factories: string[]
[k: string]: unknown
}
export interface GroupIdParam {
id: string
name: string
[k: string]: unknown
}
export interface SetFactoryArrayBody {
type: 'ignored' | 'base'
factories: string[]
[k: string]: unknown
}
export interface UploadDataBody {
groups: {
[k: string]: {
name: string
exports: string[]
malls: string[]
}
}
ignored: string[]
base: string[]
}
export interface IdParam {
id: string
[k: string]: unknown
}

View File

@@ -27,9 +27,10 @@ export async function addSchema(schemas: JSONSchema[]) {
// compile schema to interface
let entity = await compile(schema, schema.id, {
style,
bannerComment: data === '' ? undefined : '\n\n\n'
bannerComment: data === '' ? undefined : '\n\n\n',
additionalProperties: false
})
entity = entity.replace(/^( {2})+\[k: string]: unknown\n/gm, '')
//entity = entity.replace(/^( {2})+\[k: string]: unknown\n/gm, '')
dataFrontend += entity
// remove ? from defaulted properties
const defaults = getDefaults(schema)

View File

@@ -38,6 +38,26 @@ export function addSchemas() {
factories: { type: 'array', items: { type: 'string', minLength: 3 } }
}
},
{
id: '/UploadDataBody',
type: 'object',
required: ['groups', 'ignored', 'base'],
properties: {
groups: { type: 'object',
additionalProperties: {
type: 'object',
required: ['name', 'exports', 'malls'],
properties: {
name: {type: 'string', minLength: 1},
exports: {type: 'array', items: {type: 'string', minLength: 3}},
malls: {type: 'array', items: {type: 'string', minLength: 3}}
},
}
},
ignored: { type: 'array', items: { type: 'string', minLength: 3 } },
base: { type: 'array', items: { type: 'string', minLength: 3 } }
}
},
{
id: 'IdParam',
type: 'object',