completed invitation and changed otas
This commit is contained in:
parent
5debcfebe8
commit
ee10e75464
3 changed files with 56 additions and 14 deletions
|
@ -212,19 +212,47 @@ export const getInvitations: Act = {
|
|||
await remove(invitations)
|
||||
.where(leq(invitations.expires, uts()))
|
||||
.query(db);
|
||||
let req = await select([invitations.room, invitations.server, invitations.ota, invitations.expires], invitations)
|
||||
let req = await select([
|
||||
invitations.invitationID,
|
||||
invitations.room,
|
||||
invitations.server,
|
||||
invitations.ota,
|
||||
invitations.expires,
|
||||
invitations.fromName,
|
||||
invitations.fromServer,
|
||||
], invitations)
|
||||
.where(eq(invitations.accID, client.accID))
|
||||
.query(db);
|
||||
let out = req.map(d => {
|
||||
let invitationID = d[invitations.invitationID];
|
||||
let room = d[invitations.room];
|
||||
let server = d[invitations.server];
|
||||
let ota = d[invitations.ota];
|
||||
let expires = d[invitations.expires];
|
||||
if (room != null && server != null && ota != null && expires != null) {
|
||||
return { room, server, ota, expires };
|
||||
let fromName = d[invitations.fromName];
|
||||
let fromServer = d[invitations.fromServer];
|
||||
if (invitationID != null && room != null && server != null && ota != null && expires != null && fromName != null && fromServer != null) {
|
||||
return { invitationID, room, server, ota, expires, fromName, fromServer };
|
||||
}
|
||||
return null;
|
||||
});
|
||||
aws("ok", out.filter(d => d != null));
|
||||
}
|
||||
};
|
||||
|
||||
export const deleteInvitation: Act = {
|
||||
state: STATE.client,
|
||||
right: PERMISSIONS.CAN_USE_API,
|
||||
data: {
|
||||
invitationID: "number",
|
||||
},
|
||||
func: async (client: Client, data: any, aws: (code: string, data: any) => void) => {
|
||||
let req = await remove(invitations)
|
||||
.where(and(
|
||||
eq(invitations.accID, client.accID),
|
||||
eq(invitations.invitationID, data.invitationID),
|
||||
)).query(db);
|
||||
if (req.affectedRows > 0) aws("ok", "");
|
||||
else aws("error", "existence");
|
||||
}
|
||||
};
|
|
@ -303,26 +303,30 @@ export const getRoomOTAs: Act = {
|
|||
}
|
||||
let roomID = await client.isRoomAdmin(data.room, ROOM_RIGHTS.OTA);
|
||||
if (roomID == -1) return void aws("error", "roomAdmin");
|
||||
let req = await select([roomOTAs.token, roomOTAs.expires, roomOTAs.usesLeft], roomOTAs)
|
||||
let req = await select([roomOTAs.token, roomOTAs.name, roomOTAs.expires, roomOTAs.usesLeft, roomOTAs.isInvitation], roomOTAs)
|
||||
.where(eq(roomOTAs.roomID, roomID))
|
||||
.query(db);
|
||||
aws("ok", req.map(d => ({
|
||||
token: d[roomOTAs.token],
|
||||
name: d[roomOTAs.name],
|
||||
expires: d[roomOTAs.expires],
|
||||
usesLeft: d[roomOTAs.usesLeft]
|
||||
usesLeft: d[roomOTAs.usesLeft],
|
||||
isInvitation: d[roomOTAs.isInvitation],
|
||||
})));
|
||||
}
|
||||
};
|
||||
|
||||
export const addRoomOTA: Act = {
|
||||
export const addRoomOTA: Act = { // or change it, primary key is room and token
|
||||
state: STATE.client | STATE.remote,
|
||||
right: 0,
|
||||
data: {
|
||||
room: "string",
|
||||
server: "string",
|
||||
token: "string-255",
|
||||
token: "string-256",
|
||||
name: "string-256",
|
||||
expires: "number",
|
||||
usesLeft: "number",
|
||||
isInvitation: "boolean"
|
||||
},
|
||||
func: async (client: Client, data: any, aws: (code: string, data: any) => void) => {
|
||||
if (!checkSelfTag(data.server)) {
|
||||
|
@ -334,13 +338,15 @@ export const addRoomOTA: Act = {
|
|||
let roomID = await client.isRoomAdmin(data.room, ROOM_RIGHTS.OTA);
|
||||
if (roomID == -1) return void aws("error", "roomAdmin");
|
||||
try {
|
||||
await insert(roomOTAs.roomID, roomOTAs.token, roomOTAs.expires, roomOTAs.usesLeft)
|
||||
.add(roomID, data.token, data.expires, data.usesLeft)
|
||||
await insert(roomOTAs.roomID, roomOTAs.token, roomOTAs.name, roomOTAs.expires, roomOTAs.usesLeft, roomOTAs.isInvitation)
|
||||
.add(roomID, data.token, data.name, data.expires, data.usesLeft, data.isInvitation)
|
||||
.query(db);
|
||||
} catch (error) {
|
||||
await update(roomOTAs)
|
||||
.set(roomOTAs.expires, data.expires)
|
||||
.set(roomOTAs.usesLeft, data.usesLeft)
|
||||
.set(roomOTAs.name, data.name)
|
||||
.set(roomOTAs.isInvitation, data.isInvitation)
|
||||
.where(and(
|
||||
eq(roomOTAs.token, data.token),
|
||||
eq(roomOTAs.roomID, roomID)
|
||||
|
@ -409,13 +415,17 @@ export const invite: Act = {
|
|||
invitations.room,
|
||||
invitations.server,
|
||||
invitations.ota,
|
||||
invitations.expires
|
||||
invitations.expires,
|
||||
invitations.fromName,
|
||||
invitations.fromServer
|
||||
).add(
|
||||
resp[0].accID,
|
||||
data.room,
|
||||
roomServer.tag,
|
||||
data.token,
|
||||
Math.min(data.expires, uts() + 60 * 60 * 24 * 10)
|
||||
Math.min(data.expires, uts() + 60 * 60 * 24 * 10),
|
||||
client.name,
|
||||
client.server.tag,
|
||||
).query(db);
|
||||
|
||||
} catch (error) {
|
||||
|
|
|
@ -96,6 +96,8 @@ invitations.addAttributes({
|
|||
server: { type: VARCHAR(256) },
|
||||
ota: { type: VARCHAR(128) },
|
||||
expires: { type: BIGINT },
|
||||
fromName: { type: VARCHAR(256) },
|
||||
fromServer: { type: VARCHAR(256) },
|
||||
});
|
||||
|
||||
export const settings = db.newTable("settings");
|
||||
|
@ -168,12 +170,14 @@ roomOTAs.addAttributes({
|
|||
onUpdate: onAction.cascade
|
||||
}
|
||||
},
|
||||
name: { type: VARCHAR(256) },
|
||||
token: {
|
||||
type: VARCHAR(128),
|
||||
type: VARCHAR(256),
|
||||
primaryKey: true
|
||||
},
|
||||
expires: { type: BIGINT },
|
||||
usesLeft: { type: INT }
|
||||
usesLeft: { type: INT },
|
||||
isInvitation: { type: BOOL },
|
||||
});
|
||||
|
||||
export const listCategories = db.newTable("listCategories");
|
||||
|
@ -250,7 +254,7 @@ listItems.addAttributes({
|
|||
unit: { type: SMALLINT },
|
||||
value: { type: TEXT },
|
||||
link: {
|
||||
type:INT,
|
||||
type: INT,
|
||||
foreignKey: {
|
||||
link: listProducts.listProdID,
|
||||
onDelete: onAction.setNull,
|
||||
|
|
Loading…
Reference in a new issue