@sonatel-os/strapi-plugin-split 0.4.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +12 -12
- package/dist/server/index.js +83 -10
- package/dist/server/index.mjs +83 -10
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -8,17 +8,17 @@ It assumes nothing about its host. Who the people are (a header your gateway set
|
|
|
8
8
|
|
|
9
9
|
## What you get
|
|
10
10
|
|
|
11
|
-
| Area | In the admin | For the app (`/api/split`)
|
|
12
|
-
| --------------- | ---------------------------------------------------------------------------------------------- |
|
|
13
|
-
| **People** | Search by name or phone (masked), reveal a full number (permission, logged), deactivate. | `GET /me`, `PUT /me` (`{ autoAcceptInvites }`)
|
|
14
|
-
| **Home** | Dashboard: people, groups, amount spent together, amount settled, by method. | `GET /overview` (what to pay, who owes you, every group), `GET /activity`
|
|
15
|
-
| **Groups** | Search by name or invite code; one group's books: seats, balances, payments, history. Archive. | `POST /groups`, `POST /groups/join`, `GET/PATCH/PUT /groups/:group`, `…/archive`, `…/leave`
|
|
16
|
-
| **Invitations** | — | `GET /invitations`, `POST /invitations/:invitation/accept`, `…/decline`
|
|
17
|
-
| **Members** | Seats with their state: uses the app, added by name, left. | `POST /groups/:group/members`, `DELETE /groups/:group/members/:member`
|
|
18
|
-
| **Expenses** | The most recent fifty of a group. | `GET/POST /groups/:group/expenses`, `PUT/DELETE …/expenses/:expense`
|
|
19
|
-
| **Settlements** | Look a payment up by the reference the customer has. | `GET/POST /groups/:group/settlements`, `POST …/settlements/:settlement/confirm`, `…/reject`, `…/cancel`
|
|
20
|
-
| **Orders** | — (each one is in the audit trail; a closed one is an expense). | `GET/POST /groups/:group/orders`, `GET …/orders/:order`, `PUT …/orders/:order/lines`, `POST …/orders/:order/close`, `…/cancel` |
|
|
21
|
-
| **Audit** | Who did what in which group, newest first. | —
|
|
11
|
+
| Area | In the admin | For the app (`/api/split`) |
|
|
12
|
+
| --------------- | ---------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
13
|
+
| **People** | Search by name or phone (masked), reveal a full number (permission, logged), deactivate. | `GET /me`, `PUT /me` (`{ autoAcceptInvites }`) |
|
|
14
|
+
| **Home** | Dashboard: people, groups, amount spent together, amount settled, by method. | `GET /overview` (what to pay, who owes you, every group), `GET /activity` |
|
|
15
|
+
| **Groups** | Search by name or invite code; one group's books: seats, balances, payments, history. Archive. | `POST /groups`, `POST /groups/join`, `GET/PATCH/PUT /groups/:group`, `…/archive`, `…/leave` |
|
|
16
|
+
| **Invitations** | — | `GET /invitations`, `POST /invitations/:invitation/accept`, `…/decline` |
|
|
17
|
+
| **Members** | Seats with their state: uses the app, added by name, left. | `POST /groups/:group/members`, `DELETE /groups/:group/members/:member` |
|
|
18
|
+
| **Expenses** | The most recent fifty of a group. | `GET/POST /groups/:group/expenses`, `PUT/DELETE …/expenses/:expense` |
|
|
19
|
+
| **Settlements** | Look a payment up by the reference the customer has. | `GET/POST /groups/:group/settlements`, `POST …/settlements/:settlement/confirm`, `…/reject`, `…/cancel` |
|
|
20
|
+
| **Orders** | — (each one is in the audit trail; a closed one is an expense). | `GET /orders`, `GET/POST /groups/:group/orders`, `GET …/orders/:order`, `PUT …/orders/:order/lines`, `POST …/orders/:order/close`, `…/cancel` |
|
|
21
|
+
| **Audit** | Who did what in which group, newest first. | — |
|
|
22
22
|
|
|
23
23
|
All plugin tables are prefixed `split_` and hidden from the Content Manager: a ledger is not edited by hand, and the admin screens are read-mostly on purpose.
|
|
24
24
|
|
|
@@ -31,7 +31,7 @@ All plugin tables are prefixed `split_` and hidden from the Content Manager: a l
|
|
|
31
31
|
- **Seats before people.** A member can be added by name and phone before they ever open the app. A member added by name only can owe and be owed, and what they are paid needs nobody's confirmation.
|
|
32
32
|
- **Being added is an offer.** A seat added with the phone of someone who uses the app (or who opens it later) is an invitation: they see it in `GET /invitations` and in `GET /overview` (`invitations`), and the group stays closed to them until they accept. Entering the group's invite code accepts it too. Someone who set `autoAcceptInvites` is in at once. Until the answer, the seat counts like a name: the group can include it in expenses (`invite: "pending"` on the member), and what it is paid needs nobody's confirmation. Declining frees the seat when nothing is on the books for it; otherwise the seat stays as a name nobody holds (`invite: "declined"`), so the books still add up.
|
|
33
33
|
- **Nobody leaves with an open account.** Leaving a group, or being removed from it, is refused (`MEMBER_NOT_SETTLED`, with `details.balance`) while the seat owes, is owed, or has a payment awaiting confirmation.
|
|
34
|
-
- **An order is not money until it closes.** Someone lists items with a unit price and, if they want, a closing time; members say how many of each they take (`PUT …/lines` replaces what one member takes; the organiser can set anyone's, also after the closing time). Closing writes one `free` expense paid by the organiser: each member owes what they took plus an equal part of the delivery fee given at closing. The expense carries the order's reference, so closing twice writes once.
|
|
34
|
+
- **An order is not money until it closes.** Someone lists items with a unit price and, if they want, a closing time; members say how many of each they take (`PUT …/lines` replaces what one member takes; the organiser can set anyone's, also after the closing time). Closing writes one `free` expense paid by the organiser: each member owes what they took plus an equal part of the delivery fee given at closing. The expense carries the order's reference, so closing twice writes once. A line can carry a short precision (`note`, 80 characters: "sans piment"), read by everyone on the order and gone when the member sends their basket again without it. `GET /orders` gives a person the orders worth knowing about across their groups — the open ones, and those closed or dropped within the last day — each with its group, its organiser and what the person took; the overview carries the same list as `orders`. It reads no books, so a host can ask it often to tell people an order was launched or closed.
|
|
35
35
|
- **No seat, no group.** Asking for a group you are not in answers the same `404` as a group that does not exist.
|
|
36
36
|
|
|
37
37
|
## Install
|
package/dist/server/index.js
CHANGED
|
@@ -81,6 +81,8 @@ const ORDER_STATUS = Object.freeze({
|
|
|
81
81
|
const ORDER_MAX_ITEMS = 30;
|
|
82
82
|
const ORDER_MAX_QUANTITY = 99;
|
|
83
83
|
const ORDER_LIST_SIZE = 20;
|
|
84
|
+
const ORDER_NOTE_MAX = 80;
|
|
85
|
+
const ORDER_RECENT_MS = 24 * 60 * 60 * 1e3;
|
|
84
86
|
const DEFAULT_LIMITS = Object.freeze({
|
|
85
87
|
maxGroupsPerPerson: 50,
|
|
86
88
|
maxMembersPerGroup: 50,
|
|
@@ -844,7 +846,7 @@ const info = {
|
|
|
844
846
|
singularName: "order-line",
|
|
845
847
|
pluralName: "order-lines",
|
|
846
848
|
displayName: "Split Order Line",
|
|
847
|
-
description: "How many of one item one member takes."
|
|
849
|
+
description: "How many of one item one member takes, and what they ask about it."
|
|
848
850
|
};
|
|
849
851
|
const options = {
|
|
850
852
|
draftAndPublish: false
|
|
@@ -877,6 +879,10 @@ const attributes = {
|
|
|
877
879
|
type: "integer",
|
|
878
880
|
required: true,
|
|
879
881
|
min: 1
|
|
882
|
+
},
|
|
883
|
+
note: {
|
|
884
|
+
type: "string",
|
|
885
|
+
maxLength: 80
|
|
880
886
|
}
|
|
881
887
|
};
|
|
882
888
|
const schema = {
|
|
@@ -1094,6 +1100,7 @@ const app = ({ strapi }) => {
|
|
|
1094
1100
|
ok(ctx, svc("format").settlement(settlement2, mine.me, viewer(ctx)));
|
|
1095
1101
|
}),
|
|
1096
1102
|
// ── Orders ───────────────────────────────────────────────────────────
|
|
1103
|
+
currentOrders: handle(strapi, async (ctx) => ok(ctx, await svc("order").current(viewer(ctx)))),
|
|
1097
1104
|
orders: handle(
|
|
1098
1105
|
strapi,
|
|
1099
1106
|
async (ctx) => ok(ctx, await svc("order").list(await seat(ctx), viewer(ctx)))
|
|
@@ -1235,13 +1242,17 @@ const view = ({ strapi }) => {
|
|
|
1235
1242
|
const debts = perGroup.flatMap((g) => g.debts).sort((a, b) => b.amount - a.amount);
|
|
1236
1243
|
const credits = perGroup.flatMap((g) => g.credits).sort((a, b) => b.amount - a.amount);
|
|
1237
1244
|
const sum = (list) => list.reduce((total, item) => total + item.amount, 0);
|
|
1238
|
-
const invitations = await
|
|
1245
|
+
const [invitations, orders] = await Promise.all([
|
|
1246
|
+
svc("member").invitations(viewer),
|
|
1247
|
+
svc("order").current(viewer)
|
|
1248
|
+
]);
|
|
1239
1249
|
return {
|
|
1240
1250
|
owe: sum(debts),
|
|
1241
1251
|
owed: sum(credits),
|
|
1242
1252
|
debts,
|
|
1243
1253
|
credits,
|
|
1244
1254
|
invitations: invitations.map((seat) => format2.invitation(seat)),
|
|
1255
|
+
orders,
|
|
1245
1256
|
groups: perGroup.map((g) => g.summary).sort((a, b) => new Date(b.lastActivityAt ?? 0) - new Date(a.lastActivityAt ?? 0))
|
|
1246
1257
|
};
|
|
1247
1258
|
},
|
|
@@ -1480,6 +1491,7 @@ const contentApi = [
|
|
|
1480
1491
|
route$1("POST", "/groups/:group/settlements/:settlement/reject", "rejectSettlement"),
|
|
1481
1492
|
route$1("POST", "/groups/:group/settlements/:settlement/cancel", "cancelSettlement"),
|
|
1482
1493
|
// orders: priced items the members order from; closing one writes an expense
|
|
1494
|
+
route$1("GET", "/orders", "currentOrders"),
|
|
1483
1495
|
route$1("GET", "/groups/:group/orders", "orders"),
|
|
1484
1496
|
route$1("POST", "/groups/:group/orders", "createOrder"),
|
|
1485
1497
|
route$1("GET", "/groups/:group/orders/:order", "order"),
|
|
@@ -2772,8 +2784,10 @@ const orderTotals = ({ items, lines, deliveryFee = 0 }) => {
|
|
|
2772
2784
|
};
|
|
2773
2785
|
const LINE_POPULATE = { item: { select: ["id"] }, member: { select: ["id"] } };
|
|
2774
2786
|
const ORDER_POPULATE = { organizer: { select: ["id"] } };
|
|
2787
|
+
const CURRENT_POPULATE = { group: { select: ["id"] }, organizer: { populate: { person: true } } };
|
|
2775
2788
|
const iso = (date) => date ? new Date(date).toISOString() : null;
|
|
2776
|
-
const
|
|
2789
|
+
const tidy = (raw, max) => String(raw ?? "").trim().replace(/\s+/g, " ").slice(0, max);
|
|
2790
|
+
const label = (raw) => tidy(raw, LABEL_MAX);
|
|
2777
2791
|
const invalid = (message, details) => badRequest(ERROR_CODES.VALIDATION, message, details);
|
|
2778
2792
|
const expenseRefOf = (order2) => `order:${order2.documentId}`;
|
|
2779
2793
|
const order = ({ strapi }) => {
|
|
@@ -2859,7 +2873,8 @@ const order = ({ strapi }) => {
|
|
|
2859
2873
|
lines: taken.map((l) => ({
|
|
2860
2874
|
item: idOf(l.item),
|
|
2861
2875
|
member: idOf(l.member),
|
|
2862
|
-
quantity: l.quantity
|
|
2876
|
+
quantity: l.quantity,
|
|
2877
|
+
note: l.note || null
|
|
2863
2878
|
}))
|
|
2864
2879
|
};
|
|
2865
2880
|
},
|
|
@@ -2876,7 +2891,11 @@ const order = ({ strapi }) => {
|
|
|
2876
2891
|
status: order2.status
|
|
2877
2892
|
});
|
|
2878
2893
|
},
|
|
2879
|
-
/**
|
|
2894
|
+
/**
|
|
2895
|
+
* What was sent, by item row id: how many, and the precision given with it if any. Unknown
|
|
2896
|
+
* items and odd quantities are refused.
|
|
2897
|
+
* @returns {Map<number, { quantity: number, note: string|null }>}
|
|
2898
|
+
*/
|
|
2880
2899
|
quantities(input, offered) {
|
|
2881
2900
|
const byDoc = new Map(offered.map((item) => [item.documentId, item.id]));
|
|
2882
2901
|
const list = Array.isArray(input?.lines) ? input.lines : null;
|
|
@@ -2890,7 +2909,7 @@ const order = ({ strapi }) => {
|
|
|
2890
2909
|
throw invalid(`Une quantité va de 0 à ${ORDER_MAX_QUANTITY}.`, {
|
|
2891
2910
|
max: ORDER_MAX_QUANTITY
|
|
2892
2911
|
});
|
|
2893
|
-
wanted.set(item, quantity);
|
|
2912
|
+
wanted.set(item, { quantity, note: tidy(raw?.note, ORDER_NOTE_MAX) || null });
|
|
2894
2913
|
}
|
|
2895
2914
|
return wanted;
|
|
2896
2915
|
},
|
|
@@ -2899,7 +2918,7 @@ const order = ({ strapi }) => {
|
|
|
2899
2918
|
* (someone who told them, someone without the app), and still can after the closing time.
|
|
2900
2919
|
* @param {{ group: Object, me: Object }} seat the caller's
|
|
2901
2920
|
* @param {string} documentId the order
|
|
2902
|
-
* @param {{ member?: string, lines: Array<{ item: string, quantity: number }> }} input
|
|
2921
|
+
* @param {{ member?: string, lines: Array<{ item: string, quantity: number, note?: string }> }} input
|
|
2903
2922
|
*/
|
|
2904
2923
|
async setLines({ group: group2, me }, documentId, input) {
|
|
2905
2924
|
const order2 = await this.find(group2, documentId);
|
|
@@ -2916,10 +2935,10 @@ const order = ({ strapi }) => {
|
|
|
2916
2935
|
const wanted = this.quantities(input, offered);
|
|
2917
2936
|
await strapi.db.transaction(async () => {
|
|
2918
2937
|
await lines().deleteMany({ where: { order: order2.id, member: target.id } });
|
|
2919
|
-
for (const [item, quantity] of wanted)
|
|
2938
|
+
for (const [item, { quantity, note }] of wanted)
|
|
2920
2939
|
if (quantity > 0)
|
|
2921
2940
|
await lines().create({
|
|
2922
|
-
data: { order: order2.id, item, member: target.id, quantity }
|
|
2941
|
+
data: { order: order2.id, item, member: target.id, quantity, note }
|
|
2923
2942
|
});
|
|
2924
2943
|
});
|
|
2925
2944
|
return order2;
|
|
@@ -3034,7 +3053,7 @@ const order = ({ strapi }) => {
|
|
|
3034
3053
|
yours
|
|
3035
3054
|
};
|
|
3036
3055
|
if (!detailed) return summary;
|
|
3037
|
-
const takenBy = (member2) => taken.filter((l) => l.member === member2 && docOf.has(l.item)).map((l) => ({ item: docOf.get(l.item), quantity: l.quantity }));
|
|
3056
|
+
const takenBy = (member2) => taken.filter((l) => l.member === member2 && docOf.has(l.item)).map((l) => ({ item: docOf.get(l.item), quantity: l.quantity, note: l.note }));
|
|
3038
3057
|
const quantityOf = (item, member2) => taken.filter((l) => l.item === item && (member2 == null || l.member === member2)).reduce((sum, l) => sum + l.quantity, 0);
|
|
3039
3058
|
return {
|
|
3040
3059
|
...summary,
|
|
@@ -3052,6 +3071,60 @@ const order = ({ strapi }) => {
|
|
|
3052
3071
|
}))
|
|
3053
3072
|
};
|
|
3054
3073
|
},
|
|
3074
|
+
/**
|
|
3075
|
+
* The orders a person should know about, across their groups: the open ones, and those that
|
|
3076
|
+
* closed or were dropped within the last day. Light enough to be asked often: no books read.
|
|
3077
|
+
* @param {Object} viewer the person asking
|
|
3078
|
+
* @param {Date} [now]
|
|
3079
|
+
* @returns {Promise<Object[]>} the latest first
|
|
3080
|
+
*/
|
|
3081
|
+
async current(viewer, now = /* @__PURE__ */ new Date()) {
|
|
3082
|
+
const seats = await svc("group").seatsOf(viewer);
|
|
3083
|
+
if (seats.length === 0) return [];
|
|
3084
|
+
const seatByGroup = new Map(seats.map((s) => [s.group.id, s]));
|
|
3085
|
+
const rows = await orders().findMany({
|
|
3086
|
+
where: {
|
|
3087
|
+
group: { id: { $in: [...seatByGroup.keys()] } },
|
|
3088
|
+
$or: [
|
|
3089
|
+
{ status: ORDER_STATUS.OPEN },
|
|
3090
|
+
{ closedAt: { $gte: new Date(now.getTime() - ORDER_RECENT_MS) } }
|
|
3091
|
+
]
|
|
3092
|
+
},
|
|
3093
|
+
populate: CURRENT_POPULATE,
|
|
3094
|
+
orderBy: [{ id: "desc" }]
|
|
3095
|
+
});
|
|
3096
|
+
return Promise.all(
|
|
3097
|
+
rows.map((order2) => this.brief(order2, seatByGroup.get(idOf(order2.group)), viewer))
|
|
3098
|
+
);
|
|
3099
|
+
},
|
|
3100
|
+
/**
|
|
3101
|
+
* An order in a few facts, for someone who is not looking at it: where, whose, where it
|
|
3102
|
+
* stands and what the viewer took.
|
|
3103
|
+
* @param {Object} order `organizer.person` populated
|
|
3104
|
+
* @param {Object} me the viewer's seat, `group` populated
|
|
3105
|
+
* @param {Object} viewer the person asking
|
|
3106
|
+
*/
|
|
3107
|
+
async brief(order2, me, viewer) {
|
|
3108
|
+
const { items: offered, lines: taken } = await this.load(order2);
|
|
3109
|
+
const totals = orderTotals({ items: offered, lines: taken, deliveryFee: order2.deliveryFee });
|
|
3110
|
+
const none = { quantity: 0, subtotal: 0, delivery: 0, total: 0 };
|
|
3111
|
+
const { member: _mine, ...yours } = totals.people.find((p) => p.member === me.id) ?? none;
|
|
3112
|
+
return {
|
|
3113
|
+
id: order2.documentId,
|
|
3114
|
+
group: { id: me.group.documentId, name: me.group.name },
|
|
3115
|
+
title: order2.title,
|
|
3116
|
+
status: order2.status,
|
|
3117
|
+
openedAt: iso(order2.createdAt),
|
|
3118
|
+
closesAt: iso(order2.closesAt),
|
|
3119
|
+
closedAt: iso(order2.closedAt),
|
|
3120
|
+
acceptsOrders: this.accepts(order2),
|
|
3121
|
+
isOrganizer: idOf(order2.organizer) === me.id,
|
|
3122
|
+
organizer: order2.organizer ? svc("format").member(order2.organizer, viewer) : null,
|
|
3123
|
+
participantCount: totals.people.length,
|
|
3124
|
+
total: totals.total,
|
|
3125
|
+
yours
|
|
3126
|
+
};
|
|
3127
|
+
},
|
|
3055
3128
|
/** The group's orders as a member reads them: the open ones first, then the latest. */
|
|
3056
3129
|
async list(seat, viewer) {
|
|
3057
3130
|
const rows = await orders().findMany({
|
package/dist/server/index.mjs
CHANGED
|
@@ -80,6 +80,8 @@ const ORDER_STATUS = Object.freeze({
|
|
|
80
80
|
const ORDER_MAX_ITEMS = 30;
|
|
81
81
|
const ORDER_MAX_QUANTITY = 99;
|
|
82
82
|
const ORDER_LIST_SIZE = 20;
|
|
83
|
+
const ORDER_NOTE_MAX = 80;
|
|
84
|
+
const ORDER_RECENT_MS = 24 * 60 * 60 * 1e3;
|
|
83
85
|
const DEFAULT_LIMITS = Object.freeze({
|
|
84
86
|
maxGroupsPerPerson: 50,
|
|
85
87
|
maxMembersPerGroup: 50,
|
|
@@ -843,7 +845,7 @@ const info = {
|
|
|
843
845
|
singularName: "order-line",
|
|
844
846
|
pluralName: "order-lines",
|
|
845
847
|
displayName: "Split Order Line",
|
|
846
|
-
description: "How many of one item one member takes."
|
|
848
|
+
description: "How many of one item one member takes, and what they ask about it."
|
|
847
849
|
};
|
|
848
850
|
const options = {
|
|
849
851
|
draftAndPublish: false
|
|
@@ -876,6 +878,10 @@ const attributes = {
|
|
|
876
878
|
type: "integer",
|
|
877
879
|
required: true,
|
|
878
880
|
min: 1
|
|
881
|
+
},
|
|
882
|
+
note: {
|
|
883
|
+
type: "string",
|
|
884
|
+
maxLength: 80
|
|
879
885
|
}
|
|
880
886
|
};
|
|
881
887
|
const schema = {
|
|
@@ -1093,6 +1099,7 @@ const app = ({ strapi }) => {
|
|
|
1093
1099
|
ok(ctx, svc("format").settlement(settlement2, mine.me, viewer(ctx)));
|
|
1094
1100
|
}),
|
|
1095
1101
|
// ── Orders ───────────────────────────────────────────────────────────
|
|
1102
|
+
currentOrders: handle(strapi, async (ctx) => ok(ctx, await svc("order").current(viewer(ctx)))),
|
|
1096
1103
|
orders: handle(
|
|
1097
1104
|
strapi,
|
|
1098
1105
|
async (ctx) => ok(ctx, await svc("order").list(await seat(ctx), viewer(ctx)))
|
|
@@ -1234,13 +1241,17 @@ const view = ({ strapi }) => {
|
|
|
1234
1241
|
const debts = perGroup.flatMap((g) => g.debts).sort((a, b) => b.amount - a.amount);
|
|
1235
1242
|
const credits = perGroup.flatMap((g) => g.credits).sort((a, b) => b.amount - a.amount);
|
|
1236
1243
|
const sum = (list) => list.reduce((total, item) => total + item.amount, 0);
|
|
1237
|
-
const invitations = await
|
|
1244
|
+
const [invitations, orders] = await Promise.all([
|
|
1245
|
+
svc("member").invitations(viewer),
|
|
1246
|
+
svc("order").current(viewer)
|
|
1247
|
+
]);
|
|
1238
1248
|
return {
|
|
1239
1249
|
owe: sum(debts),
|
|
1240
1250
|
owed: sum(credits),
|
|
1241
1251
|
debts,
|
|
1242
1252
|
credits,
|
|
1243
1253
|
invitations: invitations.map((seat) => format2.invitation(seat)),
|
|
1254
|
+
orders,
|
|
1244
1255
|
groups: perGroup.map((g) => g.summary).sort((a, b) => new Date(b.lastActivityAt ?? 0) - new Date(a.lastActivityAt ?? 0))
|
|
1245
1256
|
};
|
|
1246
1257
|
},
|
|
@@ -1479,6 +1490,7 @@ const contentApi = [
|
|
|
1479
1490
|
route$1("POST", "/groups/:group/settlements/:settlement/reject", "rejectSettlement"),
|
|
1480
1491
|
route$1("POST", "/groups/:group/settlements/:settlement/cancel", "cancelSettlement"),
|
|
1481
1492
|
// orders: priced items the members order from; closing one writes an expense
|
|
1493
|
+
route$1("GET", "/orders", "currentOrders"),
|
|
1482
1494
|
route$1("GET", "/groups/:group/orders", "orders"),
|
|
1483
1495
|
route$1("POST", "/groups/:group/orders", "createOrder"),
|
|
1484
1496
|
route$1("GET", "/groups/:group/orders/:order", "order"),
|
|
@@ -2771,8 +2783,10 @@ const orderTotals = ({ items, lines, deliveryFee = 0 }) => {
|
|
|
2771
2783
|
};
|
|
2772
2784
|
const LINE_POPULATE = { item: { select: ["id"] }, member: { select: ["id"] } };
|
|
2773
2785
|
const ORDER_POPULATE = { organizer: { select: ["id"] } };
|
|
2786
|
+
const CURRENT_POPULATE = { group: { select: ["id"] }, organizer: { populate: { person: true } } };
|
|
2774
2787
|
const iso = (date) => date ? new Date(date).toISOString() : null;
|
|
2775
|
-
const
|
|
2788
|
+
const tidy = (raw, max) => String(raw ?? "").trim().replace(/\s+/g, " ").slice(0, max);
|
|
2789
|
+
const label = (raw) => tidy(raw, LABEL_MAX);
|
|
2776
2790
|
const invalid = (message, details) => badRequest(ERROR_CODES.VALIDATION, message, details);
|
|
2777
2791
|
const expenseRefOf = (order2) => `order:${order2.documentId}`;
|
|
2778
2792
|
const order = ({ strapi }) => {
|
|
@@ -2858,7 +2872,8 @@ const order = ({ strapi }) => {
|
|
|
2858
2872
|
lines: taken.map((l) => ({
|
|
2859
2873
|
item: idOf(l.item),
|
|
2860
2874
|
member: idOf(l.member),
|
|
2861
|
-
quantity: l.quantity
|
|
2875
|
+
quantity: l.quantity,
|
|
2876
|
+
note: l.note || null
|
|
2862
2877
|
}))
|
|
2863
2878
|
};
|
|
2864
2879
|
},
|
|
@@ -2875,7 +2890,11 @@ const order = ({ strapi }) => {
|
|
|
2875
2890
|
status: order2.status
|
|
2876
2891
|
});
|
|
2877
2892
|
},
|
|
2878
|
-
/**
|
|
2893
|
+
/**
|
|
2894
|
+
* What was sent, by item row id: how many, and the precision given with it if any. Unknown
|
|
2895
|
+
* items and odd quantities are refused.
|
|
2896
|
+
* @returns {Map<number, { quantity: number, note: string|null }>}
|
|
2897
|
+
*/
|
|
2879
2898
|
quantities(input, offered) {
|
|
2880
2899
|
const byDoc = new Map(offered.map((item) => [item.documentId, item.id]));
|
|
2881
2900
|
const list = Array.isArray(input?.lines) ? input.lines : null;
|
|
@@ -2889,7 +2908,7 @@ const order = ({ strapi }) => {
|
|
|
2889
2908
|
throw invalid(`Une quantité va de 0 à ${ORDER_MAX_QUANTITY}.`, {
|
|
2890
2909
|
max: ORDER_MAX_QUANTITY
|
|
2891
2910
|
});
|
|
2892
|
-
wanted.set(item, quantity);
|
|
2911
|
+
wanted.set(item, { quantity, note: tidy(raw?.note, ORDER_NOTE_MAX) || null });
|
|
2893
2912
|
}
|
|
2894
2913
|
return wanted;
|
|
2895
2914
|
},
|
|
@@ -2898,7 +2917,7 @@ const order = ({ strapi }) => {
|
|
|
2898
2917
|
* (someone who told them, someone without the app), and still can after the closing time.
|
|
2899
2918
|
* @param {{ group: Object, me: Object }} seat the caller's
|
|
2900
2919
|
* @param {string} documentId the order
|
|
2901
|
-
* @param {{ member?: string, lines: Array<{ item: string, quantity: number }> }} input
|
|
2920
|
+
* @param {{ member?: string, lines: Array<{ item: string, quantity: number, note?: string }> }} input
|
|
2902
2921
|
*/
|
|
2903
2922
|
async setLines({ group: group2, me }, documentId, input) {
|
|
2904
2923
|
const order2 = await this.find(group2, documentId);
|
|
@@ -2915,10 +2934,10 @@ const order = ({ strapi }) => {
|
|
|
2915
2934
|
const wanted = this.quantities(input, offered);
|
|
2916
2935
|
await strapi.db.transaction(async () => {
|
|
2917
2936
|
await lines().deleteMany({ where: { order: order2.id, member: target.id } });
|
|
2918
|
-
for (const [item, quantity] of wanted)
|
|
2937
|
+
for (const [item, { quantity, note }] of wanted)
|
|
2919
2938
|
if (quantity > 0)
|
|
2920
2939
|
await lines().create({
|
|
2921
|
-
data: { order: order2.id, item, member: target.id, quantity }
|
|
2940
|
+
data: { order: order2.id, item, member: target.id, quantity, note }
|
|
2922
2941
|
});
|
|
2923
2942
|
});
|
|
2924
2943
|
return order2;
|
|
@@ -3033,7 +3052,7 @@ const order = ({ strapi }) => {
|
|
|
3033
3052
|
yours
|
|
3034
3053
|
};
|
|
3035
3054
|
if (!detailed) return summary;
|
|
3036
|
-
const takenBy = (member2) => taken.filter((l) => l.member === member2 && docOf.has(l.item)).map((l) => ({ item: docOf.get(l.item), quantity: l.quantity }));
|
|
3055
|
+
const takenBy = (member2) => taken.filter((l) => l.member === member2 && docOf.has(l.item)).map((l) => ({ item: docOf.get(l.item), quantity: l.quantity, note: l.note }));
|
|
3037
3056
|
const quantityOf = (item, member2) => taken.filter((l) => l.item === item && (member2 == null || l.member === member2)).reduce((sum, l) => sum + l.quantity, 0);
|
|
3038
3057
|
return {
|
|
3039
3058
|
...summary,
|
|
@@ -3051,6 +3070,60 @@ const order = ({ strapi }) => {
|
|
|
3051
3070
|
}))
|
|
3052
3071
|
};
|
|
3053
3072
|
},
|
|
3073
|
+
/**
|
|
3074
|
+
* The orders a person should know about, across their groups: the open ones, and those that
|
|
3075
|
+
* closed or were dropped within the last day. Light enough to be asked often: no books read.
|
|
3076
|
+
* @param {Object} viewer the person asking
|
|
3077
|
+
* @param {Date} [now]
|
|
3078
|
+
* @returns {Promise<Object[]>} the latest first
|
|
3079
|
+
*/
|
|
3080
|
+
async current(viewer, now = /* @__PURE__ */ new Date()) {
|
|
3081
|
+
const seats = await svc("group").seatsOf(viewer);
|
|
3082
|
+
if (seats.length === 0) return [];
|
|
3083
|
+
const seatByGroup = new Map(seats.map((s) => [s.group.id, s]));
|
|
3084
|
+
const rows = await orders().findMany({
|
|
3085
|
+
where: {
|
|
3086
|
+
group: { id: { $in: [...seatByGroup.keys()] } },
|
|
3087
|
+
$or: [
|
|
3088
|
+
{ status: ORDER_STATUS.OPEN },
|
|
3089
|
+
{ closedAt: { $gte: new Date(now.getTime() - ORDER_RECENT_MS) } }
|
|
3090
|
+
]
|
|
3091
|
+
},
|
|
3092
|
+
populate: CURRENT_POPULATE,
|
|
3093
|
+
orderBy: [{ id: "desc" }]
|
|
3094
|
+
});
|
|
3095
|
+
return Promise.all(
|
|
3096
|
+
rows.map((order2) => this.brief(order2, seatByGroup.get(idOf(order2.group)), viewer))
|
|
3097
|
+
);
|
|
3098
|
+
},
|
|
3099
|
+
/**
|
|
3100
|
+
* An order in a few facts, for someone who is not looking at it: where, whose, where it
|
|
3101
|
+
* stands and what the viewer took.
|
|
3102
|
+
* @param {Object} order `organizer.person` populated
|
|
3103
|
+
* @param {Object} me the viewer's seat, `group` populated
|
|
3104
|
+
* @param {Object} viewer the person asking
|
|
3105
|
+
*/
|
|
3106
|
+
async brief(order2, me, viewer) {
|
|
3107
|
+
const { items: offered, lines: taken } = await this.load(order2);
|
|
3108
|
+
const totals = orderTotals({ items: offered, lines: taken, deliveryFee: order2.deliveryFee });
|
|
3109
|
+
const none = { quantity: 0, subtotal: 0, delivery: 0, total: 0 };
|
|
3110
|
+
const { member: _mine, ...yours } = totals.people.find((p) => p.member === me.id) ?? none;
|
|
3111
|
+
return {
|
|
3112
|
+
id: order2.documentId,
|
|
3113
|
+
group: { id: me.group.documentId, name: me.group.name },
|
|
3114
|
+
title: order2.title,
|
|
3115
|
+
status: order2.status,
|
|
3116
|
+
openedAt: iso(order2.createdAt),
|
|
3117
|
+
closesAt: iso(order2.closesAt),
|
|
3118
|
+
closedAt: iso(order2.closedAt),
|
|
3119
|
+
acceptsOrders: this.accepts(order2),
|
|
3120
|
+
isOrganizer: idOf(order2.organizer) === me.id,
|
|
3121
|
+
organizer: order2.organizer ? svc("format").member(order2.organizer, viewer) : null,
|
|
3122
|
+
participantCount: totals.people.length,
|
|
3123
|
+
total: totals.total,
|
|
3124
|
+
yours
|
|
3125
|
+
};
|
|
3126
|
+
},
|
|
3054
3127
|
/** The group's orders as a member reads them: the open ones first, then the latest. */
|
|
3055
3128
|
async list(seat, viewer) {
|
|
3056
3129
|
const rows = await orders().findMany({
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sonatel-os/strapi-plugin-split",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "A shared-expenses ledger for Strapi v5: groups, members, expenses split three ways, balances, settlements with idempotent writes — with admin screens and RBAC.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "LPIX-11 <mohamed.johnson@orange-sonatel.com>",
|