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)
|
await remove(invitations)
|
||||||
.where(leq(invitations.expires, uts()))
|
.where(leq(invitations.expires, uts()))
|
||||||
.query(db);
|
.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))
|
.where(eq(invitations.accID, client.accID))
|
||||||
.query(db);
|
.query(db);
|
||||||
let out = req.map(d => {
|
let out = req.map(d => {
|
||||||
|
let invitationID = d[invitations.invitationID];
|
||||||
let room = d[invitations.room];
|
let room = d[invitations.room];
|
||||||
let server = d[invitations.server];
|
let server = d[invitations.server];
|
||||||
let ota = d[invitations.ota];
|
let ota = d[invitations.ota];
|
||||||
let expires = d[invitations.expires];
|
let expires = d[invitations.expires];
|
||||||
if (room != null && server != null && ota != null && expires != null) {
|
let fromName = d[invitations.fromName];
|
||||||
return { room, server, ota, expires };
|
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;
|
return null;
|
||||||
});
|
});
|
||||||
aws("ok", out.filter(d => d != 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);
|
let roomID = await client.isRoomAdmin(data.room, ROOM_RIGHTS.OTA);
|
||||||
if (roomID == -1) return void aws("error", "roomAdmin");
|
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))
|
.where(eq(roomOTAs.roomID, roomID))
|
||||||
.query(db);
|
.query(db);
|
||||||
aws("ok", req.map(d => ({
|
aws("ok", req.map(d => ({
|
||||||
token: d[roomOTAs.token],
|
token: d[roomOTAs.token],
|
||||||
|
name: d[roomOTAs.name],
|
||||||
expires: d[roomOTAs.expires],
|
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,
|
state: STATE.client | STATE.remote,
|
||||||
right: 0,
|
right: 0,
|
||||||
data: {
|
data: {
|
||||||
room: "string",
|
room: "string",
|
||||||
server: "string",
|
server: "string",
|
||||||
token: "string-255",
|
token: "string-256",
|
||||||
|
name: "string-256",
|
||||||
expires: "number",
|
expires: "number",
|
||||||
usesLeft: "number",
|
usesLeft: "number",
|
||||||
|
isInvitation: "boolean"
|
||||||
},
|
},
|
||||||
func: async (client: Client, data: any, aws: (code: string, data: any) => void) => {
|
func: async (client: Client, data: any, aws: (code: string, data: any) => void) => {
|
||||||
if (!checkSelfTag(data.server)) {
|
if (!checkSelfTag(data.server)) {
|
||||||
|
@ -334,13 +338,15 @@ export const addRoomOTA: Act = {
|
||||||
let roomID = await client.isRoomAdmin(data.room, ROOM_RIGHTS.OTA);
|
let roomID = await client.isRoomAdmin(data.room, ROOM_RIGHTS.OTA);
|
||||||
if (roomID == -1) return void aws("error", "roomAdmin");
|
if (roomID == -1) return void aws("error", "roomAdmin");
|
||||||
try {
|
try {
|
||||||
await insert(roomOTAs.roomID, roomOTAs.token, roomOTAs.expires, roomOTAs.usesLeft)
|
await insert(roomOTAs.roomID, roomOTAs.token, roomOTAs.name, roomOTAs.expires, roomOTAs.usesLeft, roomOTAs.isInvitation)
|
||||||
.add(roomID, data.token, data.expires, data.usesLeft)
|
.add(roomID, data.token, data.name, data.expires, data.usesLeft, data.isInvitation)
|
||||||
.query(db);
|
.query(db);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
await update(roomOTAs)
|
await update(roomOTAs)
|
||||||
.set(roomOTAs.expires, data.expires)
|
.set(roomOTAs.expires, data.expires)
|
||||||
.set(roomOTAs.usesLeft, data.usesLeft)
|
.set(roomOTAs.usesLeft, data.usesLeft)
|
||||||
|
.set(roomOTAs.name, data.name)
|
||||||
|
.set(roomOTAs.isInvitation, data.isInvitation)
|
||||||
.where(and(
|
.where(and(
|
||||||
eq(roomOTAs.token, data.token),
|
eq(roomOTAs.token, data.token),
|
||||||
eq(roomOTAs.roomID, roomID)
|
eq(roomOTAs.roomID, roomID)
|
||||||
|
@ -409,13 +415,17 @@ export const invite: Act = {
|
||||||
invitations.room,
|
invitations.room,
|
||||||
invitations.server,
|
invitations.server,
|
||||||
invitations.ota,
|
invitations.ota,
|
||||||
invitations.expires
|
invitations.expires,
|
||||||
|
invitations.fromName,
|
||||||
|
invitations.fromServer
|
||||||
).add(
|
).add(
|
||||||
resp[0].accID,
|
resp[0].accID,
|
||||||
data.room,
|
data.room,
|
||||||
roomServer.tag,
|
roomServer.tag,
|
||||||
data.token,
|
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);
|
).query(db);
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|
|
@ -96,6 +96,8 @@ invitations.addAttributes({
|
||||||
server: { type: VARCHAR(256) },
|
server: { type: VARCHAR(256) },
|
||||||
ota: { type: VARCHAR(128) },
|
ota: { type: VARCHAR(128) },
|
||||||
expires: { type: BIGINT },
|
expires: { type: BIGINT },
|
||||||
|
fromName: { type: VARCHAR(256) },
|
||||||
|
fromServer: { type: VARCHAR(256) },
|
||||||
});
|
});
|
||||||
|
|
||||||
export const settings = db.newTable("settings");
|
export const settings = db.newTable("settings");
|
||||||
|
@ -168,12 +170,14 @@ roomOTAs.addAttributes({
|
||||||
onUpdate: onAction.cascade
|
onUpdate: onAction.cascade
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
name: { type: VARCHAR(256) },
|
||||||
token: {
|
token: {
|
||||||
type: VARCHAR(128),
|
type: VARCHAR(256),
|
||||||
primaryKey: true
|
primaryKey: true
|
||||||
},
|
},
|
||||||
expires: { type: BIGINT },
|
expires: { type: BIGINT },
|
||||||
usesLeft: { type: INT }
|
usesLeft: { type: INT },
|
||||||
|
isInvitation: { type: BOOL },
|
||||||
});
|
});
|
||||||
|
|
||||||
export const listCategories = db.newTable("listCategories");
|
export const listCategories = db.newTable("listCategories");
|
||||||
|
@ -250,7 +254,7 @@ listItems.addAttributes({
|
||||||
unit: { type: SMALLINT },
|
unit: { type: SMALLINT },
|
||||||
value: { type: TEXT },
|
value: { type: TEXT },
|
||||||
link: {
|
link: {
|
||||||
type:INT,
|
type: INT,
|
||||||
foreignKey: {
|
foreignKey: {
|
||||||
link: listProducts.listProdID,
|
link: listProducts.listProdID,
|
||||||
onDelete: onAction.setNull,
|
onDelete: onAction.setNull,
|
||||||
|
|
Loading…
Reference in a new issue