This commit is contained in:
Sebastian Seedorf
2022-08-18 09:20:00 +02:00
parent 92b762bbd2
commit de95f57b18
60 changed files with 3450 additions and 994 deletions

View File

@@ -1,22 +1,22 @@
import {database} from "./start";
import {Dict, Group} from "../types";
import {Filter, ObjectId} from "mongodb";
import { database } from './start'
import { Dict, Group } from '../types'
import { Filter, ObjectId } from 'mongodb'
export interface GroupData {
groups: Dict<Group>,
ignored: string[],
groups: Dict<Group>
ignored: string[]
base: string[]
}
export type InsertMeta<T> = T & {
createdOn: Date,
modifiedOn: Date,
createdOn: Date
modifiedOn: Date
accessedOn: Date
}
type GroupFilter = Filter<InsertMeta<GroupData>>
export async function setGroups(data: GroupData): Promise<string|undefined> {
export async function setGroups(data: GroupData): Promise<string | undefined> {
const collection = (await database.resolve())?.collection('setups')
if (!collection) return
const result = await collection.insertOne({
@@ -25,7 +25,6 @@ export async function setGroups(data: GroupData): Promise<string|undefined> {
modifiedOn: new Date(),
accessedOn: new Date()
})
console.log(result.insertedId, result.insertedId.toString())
return result.insertedId.toString()
}
@@ -35,38 +34,48 @@ function getUuid(uuid: string): GroupFilter {
}
}
export async function setFactories(uuid: string, type: 'ignored'|'base', factories: string[]): Promise<boolean> {
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})
collection.updateOne(getUuid(uuid), { $set: { [type]: factories } as never })
return true
}
export async function getGroup(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()}})
await collection.updateOne(getUuid(uuid), { $set: { accessedOn: new Date() } })
}
return data
}
export async function renameGroup(uuid: string, oldName: string, newName: string): Promise<boolean> {
export async function renameGroup(
uuid: string,
oldName: string,
newName: string
): Promise<boolean> {
oldName = oldName.replace(/[.$]/g, '')
newName = newName.replace(/[.$]/g, '')
const data = await getGroup(uuid)
if (data?.groups && !(newName in data.groups)) {
const collection = (await database.resolve())?.collection('setups')
console.log("fere", `groups.${oldName}`, `groups.${newName}`)
if (!collection) return false
await collection.updateOne(getUuid(uuid), { $set: {
await collection.updateOne(getUuid(uuid), {
$set: {
[`groups.${oldName}.name`]: newName
} as never})
await collection.updateOne(getUuid(uuid), { $rename: {
} as never
})
await collection.updateOne(getUuid(uuid), {
$rename: {
[`groups.${oldName}`]: `groups.${newName}`
}})
}
})
return true
}
return false
@@ -78,7 +87,9 @@ export async function addGroup(uuid: string, name: string): Promise<boolean> {
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}})
await collection.updateOne(getUuid(uuid), {
$set: { [`groups.${name}`]: { name, exports: [], malls: [] } as never }
})
return true
}
return false
@@ -90,20 +101,26 @@ export async function removeGroup(uuid: string, name: string): Promise<boolean>
if (data?.groups && name in data.groups) {
const collection = (await database.resolve())?.collection('setups')
if (!collection) return false
console.log(`groups.${name}`)
await collection.updateOne(getUuid(uuid), {$unset: {[`groups.${name}`]: ""}})
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> {
export async function setFactoriesOfGroup(
uuid: string,
name: string,
type: 'exports' | 'malls',
factories: string[]
): Promise<boolean> {
name = name.replace(/[.$]/g, '')
const data = await getGroup(uuid)
if (data?.groups && (name in data.groups)) {
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})
await collection.updateOne(getUuid(uuid), {
$set: { [`groups.${name}.${type}`]: factories } as never
})
return true
}
return false

View File

@@ -1,23 +1,21 @@
import {Collection, Db, MongoClient} from 'mongodb'
import { Collection, Db, MongoClient } from 'mongodb'
import getConfig from 'next/config'
import {Resolvable} from "../utils/Resolvable";
import {GroupData, InsertMeta} from "./groups";
import { Resolvable } from '../utils/Resolvable'
import { GroupData, InsertMeta } from './groups'
import { logger } from '../utils/logger'
const { serverRuntimeConfig: {
MONGO_URL,
MONGO_USER,
MONGO_PASS,
MONGO_DB
} } = getConfig()
const {
serverRuntimeConfig: { MONGO_URL, MONGO_USER, MONGO_PASS, MONGO_DB }
} = getConfig()
async function getDatabase() {
const url = `mongodb://${MONGO_USER ? `${MONGO_USER}:${MONGO_PASS ?? ''}@` : ''}${MONGO_URL}`;
const client = new MongoClient(url);
await client.connect();
console.log('Connected successfully to server')
return client.db(MONGO_DB) as unknown as (Omit<Db, 'collection'> & {
collection : (_: 'setups') => Collection<InsertMeta<GroupData>>
})
const url = `mongodb://${MONGO_USER ? `${MONGO_USER}:${MONGO_PASS ?? ''}@` : ''}${MONGO_URL}`
const client = new MongoClient(url)
await client.connect()
logger.info('Connected successfully to server')
return client.db(MONGO_DB) as unknown as Omit<Db, 'collection'> & {
collection: (_: 'setups') => Collection<InsertMeta<GroupData>>
}
}
export const database = new Resolvable(getDatabase)