Improved upload
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user