146 lines
3.9 KiB
TypeScript
146 lines
3.9 KiB
TypeScript
import { database } from './start'
|
|
import { Dict, Group } from '../types'
|
|
import { Filter, ObjectId } from 'mongodb'
|
|
|
|
export interface GroupData {
|
|
groups: Dict<Group>
|
|
ignored: string[]
|
|
base: string[]
|
|
}
|
|
|
|
export type InsertMeta<T> = T & {
|
|
createdOn: Date
|
|
modifiedOn: Date
|
|
accessedOn: Date
|
|
}
|
|
|
|
type GroupFilter = Filter<InsertMeta<GroupData>>
|
|
|
|
export async function setData(
|
|
uuid: string | undefined,
|
|
data: GroupData
|
|
): Promise<string | undefined> {
|
|
const collection = (await database.resolve())?.collection('setups')
|
|
if (!collection) return
|
|
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 {
|
|
return {
|
|
_id: new ObjectId(uuid)
|
|
}
|
|
}
|
|
|
|
export async function setFactories(
|
|
uuid: string,
|
|
type: 'ignored' | 'base',
|
|
factories: string[]
|
|
): Promise<boolean> {
|
|
const collection = (await database.resolve())?.collection('setups')
|
|
if (!collection) return false
|
|
collection.updateOne(getUuid(uuid), { $set: { [type]: factories } as never })
|
|
return true
|
|
}
|
|
|
|
export async function getData(uuid: string) {
|
|
const collection = (await database.resolve())?.collection('setups')
|
|
if (!collection) return
|
|
const data = (await collection.findOne(getUuid(uuid))) ?? undefined
|
|
if (data) {
|
|
await collection.updateOne(getUuid(uuid), { $set: { accessedOn: new Date() } })
|
|
}
|
|
return data
|
|
}
|
|
|
|
export async function renameGroup(
|
|
uuid: string,
|
|
oldName: string,
|
|
newName: string
|
|
): Promise<boolean> {
|
|
oldName = oldName.replace(/[.$]/g, '')
|
|
newName = newName.replace(/[.$]/g, '')
|
|
const data = await getData(uuid)
|
|
if (data?.groups && !(newName in data.groups)) {
|
|
const collection = (await database.resolve())?.collection('setups')
|
|
if (!collection) return false
|
|
await collection.updateOne(getUuid(uuid), {
|
|
$set: {
|
|
[`groups.${oldName}.name`]: newName
|
|
} as never
|
|
})
|
|
await collection.updateOne(getUuid(uuid), {
|
|
$rename: {
|
|
[`groups.${oldName}`]: `groups.${newName}`
|
|
}
|
|
})
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
export async function getGroup(uuid: string, name: string) {
|
|
const data = await getData(uuid)
|
|
return data?.groups?.[name] ?? undefined
|
|
}
|
|
|
|
export async function addGroup(uuid: string, name: string): Promise<boolean> {
|
|
name = name.replace(/[.$]/g, '')
|
|
const data = await getData(uuid)
|
|
if (data?.groups && !(name in data.groups)) {
|
|
const collection = (await database.resolve())?.collection('setups')
|
|
if (!collection) return false
|
|
await collection.updateOne(getUuid(uuid), {
|
|
$set: { [`groups.${name}`]: { name, exports: [], malls: [] } as never }
|
|
})
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
export async function removeGroup(uuid: string, name: string): Promise<boolean> {
|
|
name = name.replace(/[.$]/g, '')
|
|
const data = await getData(uuid)
|
|
if (data?.groups && name in data.groups) {
|
|
const collection = (await database.resolve())?.collection('setups')
|
|
if (!collection) return false
|
|
await collection.updateOne(getUuid(uuid), { $unset: { [`groups.${name}`]: '' } })
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
export async function setFactoriesOfGroup(
|
|
uuid: string,
|
|
name: string,
|
|
type: 'exports' | 'malls',
|
|
factories: string[]
|
|
): Promise<boolean> {
|
|
name = name.replace(/[.$]/g, '')
|
|
const data = await getData(uuid)
|
|
if (data?.groups && name in data.groups) {
|
|
const collection = (await database.resolve())?.collection('setups')
|
|
if (!collection) return false
|
|
await collection.updateOne(getUuid(uuid), {
|
|
$set: { [`groups.${name}.${type}`]: factories } as never
|
|
})
|
|
return true
|
|
}
|
|
return false
|
|
}
|