user can join
This commit is contained in:
@@ -1,27 +1,11 @@
|
||||
import { z } from 'zod';
|
||||
|
||||
export default class Edge {
|
||||
schema = z.object({
|
||||
id: z.number(),
|
||||
from: z.string(),
|
||||
to: z.string(),
|
||||
created: z.string()
|
||||
})
|
||||
.strict()
|
||||
add(n) {
|
||||
let toPrefix = n.to.split("-")[0]
|
||||
let fromPrefix = n.from.split("-")[0]
|
||||
let type = n.type
|
||||
let prefix = `${fromPrefix}_${type}_${toPrefix}`
|
||||
|
||||
save(n, id) {
|
||||
let result = this.schema.safeParse(n)
|
||||
if(result.success) {
|
||||
try {
|
||||
super.add(id, n)
|
||||
} catch(e) {
|
||||
console.error(e)
|
||||
throw e
|
||||
}
|
||||
} else {
|
||||
console.error(result.error)
|
||||
throw new global.ServerError(400, "Invalid Member Data!");
|
||||
}
|
||||
global.db.addEdge(prefix, n)
|
||||
}
|
||||
|
||||
getByFrom(fromID) {
|
||||
|
||||
@@ -10,6 +10,7 @@ export default class MEMBER_IN_NETWORK {
|
||||
|
||||
schema = z.object({
|
||||
id: z.number(),
|
||||
type: z.string(),
|
||||
from: z.string(),
|
||||
to: z.string(),
|
||||
created: z.string()
|
||||
|
||||
@@ -22,12 +22,12 @@ let eIndices = {
|
||||
}
|
||||
|
||||
export let nodeModels = {
|
||||
Member: new Member(nIndices.MEMBER),
|
||||
Network: new Network(nIndices.NETWORK),
|
||||
Title: new Title(nIndices.TITLE),
|
||||
Payment: new Payment(nIndices.PAYMENT),
|
||||
Post: new Post(nIndices.POST),
|
||||
Conversation: new Conversation(nIndices.CONVERSATION),
|
||||
MEMBER: new Member(nIndices.MEMBER),
|
||||
NETWORK: new Network(nIndices.NETWORK),
|
||||
TITLE: new Title(nIndices.TITLE),
|
||||
PAYMENT: new Payment(nIndices.PAYMENT),
|
||||
POST: new Post(nIndices.POST),
|
||||
CONVERSATION: new Conversation(nIndices.CONVERSATION),
|
||||
DM: new DM(nIndices.DM),
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import argon2 from 'argon2';
|
||||
import { z } from 'zod';
|
||||
|
||||
export default class Member {
|
||||
export default class Member {
|
||||
prefix = "MEMBER"
|
||||
indices = null
|
||||
|
||||
@@ -9,47 +9,34 @@ export default class Member {
|
||||
this.indices = indices
|
||||
}
|
||||
|
||||
addressSchema = z.object({
|
||||
address1: z.string(),
|
||||
address2: z.string().optional(),
|
||||
zip: z.string().regex(/^\d{5}(-\d{4})?$/),
|
||||
state: z.string(),
|
||||
city: z.string()
|
||||
})
|
||||
schema = z.object({
|
||||
id: z.number(),
|
||||
email: z.string().email(),
|
||||
firstName: z.string(),
|
||||
lastName: z.string(),
|
||||
password: z.string(),
|
||||
joined: z.string(),
|
||||
address: this.addressSchema.optional()
|
||||
created: z.string()
|
||||
})
|
||||
|
||||
isHashed = (s) => {return s.startsWith("$argon2")}
|
||||
|
||||
save(member) {
|
||||
let id = `${this.prefix}-${member.id}`
|
||||
let result = this.schema.safeParse(member)
|
||||
if(result.success) {
|
||||
try {
|
||||
global.db.addNode(id, member)
|
||||
} catch(e) {
|
||||
console.error(e)
|
||||
throw e
|
||||
}
|
||||
} else {
|
||||
console.error("Failed parsing member: ", result.error)
|
||||
throw new global.ServerError(400, "Invalid Member Data!: ");
|
||||
}
|
||||
}
|
||||
|
||||
async add(newMember) {
|
||||
async add(newMember, network = null) {
|
||||
const hash = await argon2.hash(newMember.password);
|
||||
newMember.password = hash
|
||||
newMember.joined = global.currentTime()
|
||||
newMember.id = this.indices[1] - 1
|
||||
this.save(newMember)
|
||||
|
||||
try {
|
||||
global.db.addNode(this.prefix, newMember)
|
||||
if(network) {
|
||||
global.db.edge.add({
|
||||
type: "IN",
|
||||
from: `${this.prefix}-${global.db.getNextIndex(this)}`,
|
||||
to: "NETWORK-1"
|
||||
})
|
||||
}
|
||||
} catch(e) {
|
||||
console.error(e)
|
||||
throw new global.ServerError(400, "Failed to add member!");
|
||||
}
|
||||
}
|
||||
|
||||
get(id) {
|
||||
|
||||
@@ -13,7 +13,8 @@ export default class Network {
|
||||
name: z.string(),
|
||||
apps: z.array(z.string()),
|
||||
logo: z.string(),
|
||||
abbreviation: z.string()
|
||||
abbreviation: z.string(),
|
||||
created: z.string()
|
||||
})
|
||||
.strict()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user