diff --git a/src/api/acts/roomContent.ts b/src/api/acts/roomContent.ts index 8b3484c..5e8fbff 100644 --- a/src/api/acts/roomContent.ts +++ b/src/api/acts/roomContent.ts @@ -1,10 +1,11 @@ -import { and, coalesce, eq, insert, leftJoinOn, leftJoinUsing, max, order, plus, remove, select, update } from "dblang"; +import { and, coalesce, eq, insert, le, leftJoinOn, leftJoinUsing, max, order, plus, remove, select, update } from "dblang"; import { checkSelfTag } from "../../server/outbagURL.js"; import { Act, Client, STATE } from "../user.js"; import { db, listCategories, listItems, listProducts } from "../../sys/db.js"; import { isCategoryInRoom, isItemInRoom, isProductInRoom, isRoomDataFull } from "../helper.js" import { ROOM_RIGHTS } from "../../server/permissions.js"; import { act_error } from "../../server/errors.js"; +import { uts } from "../../sys/tools.js"; export const getCategories: Act = { state: STATE.client | STATE.remote, @@ -716,6 +717,7 @@ export const changeItemState: Act = { if (!isItemInRoom(roomID, data.listItemID)) return void aws("error", act_error.ITEM_NOT_EXISTS); let req = await update(listItems) .set(listItems.state, data.state) + .set(listItems.lastStateTime, uts()) .where(and( eq(listItems.listItemID, data.listItemID), eq(listItems.roomID, roomID) @@ -726,6 +728,50 @@ export const changeItemState: Act = { } }; +export const changeItemStates: Act = { + state: STATE.client | STATE.remote, + right: 0, + data: { + room: "string", + server: "string", + listItemIDs: "array-number", + changedTimes: "array-number", //in uts time sec + states: "array-number", // 0 = added; 1 = in cart/bought + }, + func: async (client: Client, data: any, aws: (code: string, data: any) => void) => { + if (!checkSelfTag(data.server)) { + if (client.state != STATE.client) return void aws("error", act_error.RECURSION); + let resp = await client.pass(data.server, "changeItemStates", data); + aws(resp.state, resp.data); + return; + } + let roomID = await client.isInRoom(data.room); + if (roomID == -1) { + aws("error", act_error.NOT_IN_ROOM); + return; + } + if ( + data.listItemIDs.length != data.changedTimes.length + || data.changedTimes.length != data.states.length + ) return void aws("error", "data"); + for (let i = 0; i < data.listItemIDs.length; i++) { + let id = data.listItemIDs[i]; + let time = data.changedTimes[i]; + let state = data.states[i] == 0 ? 0 : 1; + await update(listItems) + .set(listItems.state, state) + .set(listItems.lastStateTime, uts()) + .where(and( + eq(listItems.listItemID, id), + eq(listItems.roomID, roomID), + le(listItems.lastStateTime, time) + )) + .query(db); + } + aws("ok", ""); + } +}; + export const deleteItem: Act = { state: STATE.client | STATE.remote, right: 0, diff --git a/src/sys/db.ts b/src/sys/db.ts index b5b9607..d2230d6 100644 --- a/src/sys/db.ts +++ b/src/sys/db.ts @@ -230,6 +230,7 @@ listItems.addAttributes({ } }, state: { type: SMALLINT, default: 0 }, + lastStateTime: { type: BIGINT, default: 0 }, title: { type: TEXT }, description: { type: TEXT }, category: {