Implemented server routes
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import {database} from "./start";
|
||||
import {Dict, Group} from "../types";
|
||||
import {Filter, ObjectId, WithId} from "mongodb";
|
||||
import {Filter, ObjectId} from "mongodb";
|
||||
|
||||
export interface GroupData {
|
||||
groups: Dict<Group>,
|
||||
@@ -8,7 +8,7 @@ export interface GroupData {
|
||||
base: string[]
|
||||
}
|
||||
|
||||
type InsertMeta<T> = T & {
|
||||
export type InsertMeta<T> = T & {
|
||||
createdOn: Date,
|
||||
modifiedOn: Date,
|
||||
accessedOn: Date
|
||||
@@ -16,8 +16,8 @@ type InsertMeta<T> = T & {
|
||||
|
||||
type GroupFilter = Filter<InsertMeta<GroupData>>
|
||||
|
||||
export async function setGroups(data: GroupData) {
|
||||
const collection = (await database.resolve())?.collection<InsertMeta<GroupData>>('setups')
|
||||
export async function setGroups(data: GroupData): Promise<string|undefined> {
|
||||
const collection = (await database.resolve())?.collection('setups')
|
||||
if (!collection) return
|
||||
const result = await collection.insertOne({
|
||||
...data,
|
||||
@@ -29,16 +29,82 @@ export async function setGroups(data: GroupData) {
|
||||
return result.insertedId.toString()
|
||||
}
|
||||
|
||||
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 getGroup(uuid: string) {
|
||||
const collection = (await database.resolve())?.collection<InsertMeta<GroupData>>('setups')
|
||||
const collection = (await database.resolve())?.collection('setups')
|
||||
if (!collection) return
|
||||
console.log(uuid)
|
||||
const data = (await collection.findOne({
|
||||
_id: new ObjectId(uuid)
|
||||
} as GroupFilter)) ?? undefined
|
||||
const data = (await collection.findOne(getUuid(uuid))) ?? undefined
|
||||
if (data) {
|
||||
await collection.updateOne({_id: new ObjectId(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> {
|
||||
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: {
|
||||
[`groups.${oldName}.name`]: newName
|
||||
} as never})
|
||||
await collection.updateOne(getUuid(uuid), { $rename: {
|
||||
[`groups.${oldName}`]: `groups.${newName}`
|
||||
}})
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
export async function addGroup(uuid: string, name: string): Promise<boolean> {
|
||||
name = name.replace(/[.$]/g, '')
|
||||
const data = await getGroup(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 getGroup(uuid)
|
||||
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}`]: ""}})
|
||||
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 getGroup(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
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import {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";
|
||||
|
||||
const { serverRuntimeConfig: {
|
||||
MONGO_URL,
|
||||
@@ -9,12 +10,14 @@ const { serverRuntimeConfig: {
|
||||
MONGO_DB
|
||||
} } = getConfig()
|
||||
|
||||
async function getDatabase(): Promise<Db | undefined> {
|
||||
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)
|
||||
return client.db(MONGO_DB) as unknown as (Omit<Db, 'collection'> & {
|
||||
collection : (_: 'setups') => Collection<InsertMeta<GroupData>>
|
||||
})
|
||||
}
|
||||
|
||||
export const database = new Resolvable(getDatabase)
|
||||
|
||||
Reference in New Issue
Block a user