@sonatel-os/strapi-plugin-split 0.3.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 +15 -12
- package/dist/server/index.js +286 -28
- package/dist/server/index.mjs +286 -28
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -8,16 +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`
|
|
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
|
-
| **
|
|
17
|
-
| **
|
|
18
|
-
| **
|
|
19
|
-
| **
|
|
20
|
-
| **
|
|
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. | — |
|
|
21
22
|
|
|
22
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.
|
|
23
24
|
|
|
@@ -27,8 +28,10 @@ All plugin tables are prefixed `split_` and hidden from the Content Manager: a l
|
|
|
27
28
|
- **Written once.** `POST` an expense or a settlement with your own `clientRef`; sending it again answers the same record with `200` and `meta.replayed: true` instead of `201`. A settlement the host paid also carries the host's `reference` (a mobile-money transaction id, for instance), and the same reference is never written twice in a group. The columns are unique at the database level, so two requests at once cannot both write.
|
|
28
29
|
- **Never more than is owed.** A settlement is checked against the books inside the transaction that writes it, behind the group's lock: no more than the payer owes, no more than the receiver is owed, less what is already declared between them. The refusal says how much is left (`details.outstanding`).
|
|
29
30
|
- **Who confirms.** A method marked `instant` is one the host pays itself: confirmed at once. Otherwise the settlement is `pending` until the receiver confirms (or rejects; the payer can cancel). Pending settlements do not move balances.
|
|
30
|
-
- **Seats before people.** A member can be added by name and phone before they ever open the app
|
|
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
|
+
- **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
|
+
- **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. 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.
|
|
32
35
|
- **No seat, no group.** Asking for a group you are not in answers the same `404` as a group that does not exist.
|
|
33
36
|
|
|
34
37
|
## Install
|
package/dist/server/index.js
CHANGED
|
@@ -29,6 +29,12 @@ const RBAC = Object.freeze({
|
|
|
29
29
|
});
|
|
30
30
|
const SPLIT_MODE = Object.freeze({ EQUAL: "equal", WEIGHTED: "weighted", FREE: "free" });
|
|
31
31
|
const ROLE = Object.freeze({ OWNER: "owner", MEMBER: "member" });
|
|
32
|
+
const INVITE = Object.freeze({
|
|
33
|
+
/** Added by a member; the person has not said yes or no. */
|
|
34
|
+
PENDING: "pending",
|
|
35
|
+
/** The person said no, and the seat stays as a name because it is on the books. */
|
|
36
|
+
DECLINED: "declined"
|
|
37
|
+
});
|
|
32
38
|
const SETTLEMENT_STATUS = Object.freeze({
|
|
33
39
|
/** Declared; the receiver has not confirmed yet. Does not move balances. */
|
|
34
40
|
PENDING: "pending",
|
|
@@ -50,6 +56,7 @@ const EVENT = Object.freeze({
|
|
|
50
56
|
GROUP_ARCHIVED: "group.archived",
|
|
51
57
|
MEMBER_ADDED: "member.added",
|
|
52
58
|
MEMBER_JOINED: "member.joined",
|
|
59
|
+
MEMBER_DECLINED: "member.declined",
|
|
53
60
|
MEMBER_LEFT: "member.left",
|
|
54
61
|
MEMBER_REMOVED: "member.removed",
|
|
55
62
|
EXPENSE_CREATED: "expense.created",
|
|
@@ -74,6 +81,8 @@ const ORDER_STATUS = Object.freeze({
|
|
|
74
81
|
const ORDER_MAX_ITEMS = 30;
|
|
75
82
|
const ORDER_MAX_QUANTITY = 99;
|
|
76
83
|
const ORDER_LIST_SIZE = 20;
|
|
84
|
+
const ORDER_NOTE_MAX = 80;
|
|
85
|
+
const ORDER_RECENT_MS = 24 * 60 * 60 * 1e3;
|
|
77
86
|
const DEFAULT_LIMITS = Object.freeze({
|
|
78
87
|
maxGroupsPerPerson: 50,
|
|
79
88
|
maxMembersPerGroup: 50,
|
|
@@ -106,6 +115,7 @@ const ERROR_CODES = Object.freeze({
|
|
|
106
115
|
NOT_A_MEMBER: "NOT_A_MEMBER",
|
|
107
116
|
NOT_ALLOWED: "NOT_ALLOWED",
|
|
108
117
|
INVITE_INVALID: "INVITE_INVALID",
|
|
118
|
+
INVITATION_NOT_FOUND: "INVITATION_NOT_FOUND",
|
|
109
119
|
MEMBER_NOT_FOUND: "MEMBER_NOT_FOUND",
|
|
110
120
|
MEMBER_EXISTS: "MEMBER_EXISTS",
|
|
111
121
|
MEMBER_LIMIT: "MEMBER_LIMIT",
|
|
@@ -274,6 +284,10 @@ const attributes$9 = {
|
|
|
274
284
|
lastActiveAt: {
|
|
275
285
|
type: "datetime"
|
|
276
286
|
},
|
|
287
|
+
autoAcceptInvites: {
|
|
288
|
+
type: "boolean",
|
|
289
|
+
"default": false
|
|
290
|
+
},
|
|
277
291
|
memberships: {
|
|
278
292
|
type: "relation",
|
|
279
293
|
relation: "oneToMany",
|
|
@@ -354,7 +368,7 @@ const info$7 = {
|
|
|
354
368
|
singularName: "member",
|
|
355
369
|
pluralName: "members",
|
|
356
370
|
displayName: "Split Member",
|
|
357
|
-
description: "A seat in a group: a registered person, or someone a member added by name (and maybe phone) who has not opened the app yet."
|
|
371
|
+
description: "A seat in a group: a registered person who joined or was invited and has not answered yet, or someone a member added by name (and maybe phone) who has not opened the app yet."
|
|
358
372
|
};
|
|
359
373
|
const options$7 = {
|
|
360
374
|
draftAndPublish: false
|
|
@@ -404,6 +418,14 @@ const attributes$7 = {
|
|
|
404
418
|
},
|
|
405
419
|
leftAt: {
|
|
406
420
|
type: "datetime"
|
|
421
|
+
},
|
|
422
|
+
invitedBy: {
|
|
423
|
+
type: "relation",
|
|
424
|
+
relation: "manyToOne",
|
|
425
|
+
target: "plugin::split.person"
|
|
426
|
+
},
|
|
427
|
+
declinedAt: {
|
|
428
|
+
type: "datetime"
|
|
407
429
|
}
|
|
408
430
|
};
|
|
409
431
|
const schema$7 = {
|
|
@@ -824,7 +846,7 @@ const info = {
|
|
|
824
846
|
singularName: "order-line",
|
|
825
847
|
pluralName: "order-lines",
|
|
826
848
|
displayName: "Split Order Line",
|
|
827
|
-
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."
|
|
828
850
|
};
|
|
829
851
|
const options = {
|
|
830
852
|
draftAndPublish: false
|
|
@@ -857,6 +879,10 @@ const attributes = {
|
|
|
857
879
|
type: "integer",
|
|
858
880
|
required: true,
|
|
859
881
|
min: 1
|
|
882
|
+
},
|
|
883
|
+
note: {
|
|
884
|
+
type: "string",
|
|
885
|
+
maxLength: 80
|
|
860
886
|
}
|
|
861
887
|
};
|
|
862
888
|
const schema = {
|
|
@@ -957,12 +983,33 @@ const app = ({ strapi }) => {
|
|
|
957
983
|
me: handle(strapi, async (ctx) => {
|
|
958
984
|
ok(ctx, svc("format").person(viewer(ctx), Boolean(ctx.state.splitPersonIsNew)));
|
|
959
985
|
}),
|
|
986
|
+
updateMe: handle(strapi, async (ctx) => {
|
|
987
|
+
const person2 = await svc("person").setPreferences(viewer(ctx), body(ctx));
|
|
988
|
+
ok(ctx, svc("format").person(person2));
|
|
989
|
+
}),
|
|
960
990
|
overview: handle(strapi, async (ctx) => ok(ctx, await svc("view").overview(viewer(ctx)))),
|
|
961
991
|
activity: handle(strapi, async (ctx) => {
|
|
962
992
|
const page = parsePagination(ctx.query);
|
|
963
993
|
const { items, total } = await svc("view").activity(viewer(ctx), page);
|
|
964
994
|
ctx.body = paged(items, total, page);
|
|
965
995
|
}),
|
|
996
|
+
// ── Invitations ──────────────────────────────────────────────────────
|
|
997
|
+
invitations: handle(strapi, async (ctx) => {
|
|
998
|
+
const seats = await svc("member").invitations(viewer(ctx));
|
|
999
|
+
ok(
|
|
1000
|
+
ctx,
|
|
1001
|
+
seats.map((s) => svc("format").invitation(s))
|
|
1002
|
+
);
|
|
1003
|
+
}),
|
|
1004
|
+
acceptInvitation: handle(strapi, async (ctx) => {
|
|
1005
|
+
const group2 = await svc("member").accept(viewer(ctx), ctx.params.invitation);
|
|
1006
|
+
const mine = await svc("group").seat(viewer(ctx), group2.documentId);
|
|
1007
|
+
ok(ctx, await svc("view").group(mine, viewer(ctx)));
|
|
1008
|
+
}),
|
|
1009
|
+
declineInvitation: handle(strapi, async (ctx) => {
|
|
1010
|
+
await svc("member").decline(viewer(ctx), ctx.params.invitation);
|
|
1011
|
+
noContent(ctx);
|
|
1012
|
+
}),
|
|
966
1013
|
// ── Groups ───────────────────────────────────────────────────────────
|
|
967
1014
|
createGroup: handle(strapi, async (ctx) => {
|
|
968
1015
|
const group2 = await svc("group").create(viewer(ctx), body(ctx));
|
|
@@ -1053,6 +1100,7 @@ const app = ({ strapi }) => {
|
|
|
1053
1100
|
ok(ctx, svc("format").settlement(settlement2, mine.me, viewer(ctx)));
|
|
1054
1101
|
}),
|
|
1055
1102
|
// ── Orders ───────────────────────────────────────────────────────────
|
|
1103
|
+
currentOrders: handle(strapi, async (ctx) => ok(ctx, await svc("order").current(viewer(ctx)))),
|
|
1056
1104
|
orders: handle(
|
|
1057
1105
|
strapi,
|
|
1058
1106
|
async (ctx) => ok(ctx, await svc("order").list(await seat(ctx), viewer(ctx)))
|
|
@@ -1194,11 +1242,17 @@ const view = ({ strapi }) => {
|
|
|
1194
1242
|
const debts = perGroup.flatMap((g) => g.debts).sort((a, b) => b.amount - a.amount);
|
|
1195
1243
|
const credits = perGroup.flatMap((g) => g.credits).sort((a, b) => b.amount - a.amount);
|
|
1196
1244
|
const sum = (list) => list.reduce((total, item) => total + item.amount, 0);
|
|
1245
|
+
const [invitations, orders] = await Promise.all([
|
|
1246
|
+
svc("member").invitations(viewer),
|
|
1247
|
+
svc("order").current(viewer)
|
|
1248
|
+
]);
|
|
1197
1249
|
return {
|
|
1198
1250
|
owe: sum(debts),
|
|
1199
1251
|
owed: sum(credits),
|
|
1200
1252
|
debts,
|
|
1201
1253
|
credits,
|
|
1254
|
+
invitations: invitations.map((seat) => format2.invitation(seat)),
|
|
1255
|
+
orders,
|
|
1202
1256
|
groups: perGroup.map((g) => g.summary).sort((a, b) => new Date(b.lastActivityAt ?? 0) - new Date(a.lastActivityAt ?? 0))
|
|
1203
1257
|
};
|
|
1204
1258
|
},
|
|
@@ -1405,8 +1459,14 @@ const route$1 = (method, path, handler) => ({
|
|
|
1405
1459
|
const contentApi = [
|
|
1406
1460
|
// person
|
|
1407
1461
|
route$1("GET", "/me", "me"),
|
|
1462
|
+
// PUT, not PATCH: some app runtimes (mini-programs) cannot send PATCH.
|
|
1463
|
+
route$1("PUT", "/me", "updateMe"),
|
|
1408
1464
|
route$1("GET", "/overview", "overview"),
|
|
1409
1465
|
route$1("GET", "/activity", "activity"),
|
|
1466
|
+
// invitations: being added to a group is an offer, answered by the person
|
|
1467
|
+
route$1("GET", "/invitations", "invitations"),
|
|
1468
|
+
route$1("POST", "/invitations/:invitation/accept", "acceptInvitation"),
|
|
1469
|
+
route$1("POST", "/invitations/:invitation/decline", "declineInvitation"),
|
|
1410
1470
|
// groups
|
|
1411
1471
|
route$1("POST", "/groups", "createGroup"),
|
|
1412
1472
|
route$1("POST", "/groups/join", "joinGroup"),
|
|
@@ -1431,6 +1491,7 @@ const contentApi = [
|
|
|
1431
1491
|
route$1("POST", "/groups/:group/settlements/:settlement/reject", "rejectSettlement"),
|
|
1432
1492
|
route$1("POST", "/groups/:group/settlements/:settlement/cancel", "cancelSettlement"),
|
|
1433
1493
|
// orders: priced items the members order from; closing one writes an expense
|
|
1494
|
+
route$1("GET", "/orders", "currentOrders"),
|
|
1434
1495
|
route$1("GET", "/groups/:group/orders", "orders"),
|
|
1435
1496
|
route$1("POST", "/groups/:group/orders", "createOrder"),
|
|
1436
1497
|
route$1("GET", "/groups/:group/orders/:order", "order"),
|
|
@@ -1541,7 +1602,13 @@ const audit = ({ strapi }) => ({
|
|
|
1541
1602
|
const iso$1 = (date) => date ? new Date(date).toISOString() : null;
|
|
1542
1603
|
const format = () => ({
|
|
1543
1604
|
person(p, isNew = false) {
|
|
1544
|
-
return {
|
|
1605
|
+
return {
|
|
1606
|
+
id: p.documentId,
|
|
1607
|
+
displayName: p.displayName ?? null,
|
|
1608
|
+
phone: p.phone ?? null,
|
|
1609
|
+
autoAcceptInvites: Boolean(p.autoAcceptInvites),
|
|
1610
|
+
isNew
|
|
1611
|
+
};
|
|
1545
1612
|
},
|
|
1546
1613
|
/** The name a seat goes by: the person's own once they have one, the name it was added under otherwise. */
|
|
1547
1614
|
nameOf(member2) {
|
|
@@ -1561,10 +1628,29 @@ const format = () => ({
|
|
|
1561
1628
|
displayName: this.nameOf(m),
|
|
1562
1629
|
isYou: Boolean(viewer) && idOf(m.person) === viewer.id,
|
|
1563
1630
|
isRegistered: Boolean(m.person),
|
|
1631
|
+
invite: this.inviteOf(m),
|
|
1564
1632
|
role: m.role,
|
|
1565
1633
|
hasLeft: Boolean(m.leftAt)
|
|
1566
1634
|
};
|
|
1567
1635
|
},
|
|
1636
|
+
/** `pending` while an invited person has not answered, `declined` once they said no, else null. */
|
|
1637
|
+
inviteOf(m) {
|
|
1638
|
+
if (m.declinedAt) return INVITE.DECLINED;
|
|
1639
|
+
return m.person && !m.joinedAt ? INVITE.PENDING : null;
|
|
1640
|
+
},
|
|
1641
|
+
/**
|
|
1642
|
+
* An invitation as the invited person sees it: enough to decide, nothing of the books.
|
|
1643
|
+
* @param {Object} seat member row, `group` and `invitedBy` populated, with a `memberCount`
|
|
1644
|
+
*/
|
|
1645
|
+
invitation(seat) {
|
|
1646
|
+
return {
|
|
1647
|
+
id: seat.documentId,
|
|
1648
|
+
group: { id: seat.group.documentId, name: seat.group.name },
|
|
1649
|
+
invitedBy: seat.invitedBy ? { displayName: seat.invitedBy.displayName ?? null } : null,
|
|
1650
|
+
memberCount: seat.memberCount,
|
|
1651
|
+
invitedAt: iso$1(seat.createdAt)
|
|
1652
|
+
};
|
|
1653
|
+
},
|
|
1568
1654
|
/** A seat as an administrator sees it: the phone masked. */
|
|
1569
1655
|
adminMember(m) {
|
|
1570
1656
|
return { ...this.member(m), phone: maskPhone(this.phoneOf(m)), joinedAt: iso$1(m.joinedAt) };
|
|
@@ -1678,7 +1764,8 @@ const person = ({ strapi }) => ({
|
|
|
1678
1764
|
}
|
|
1679
1765
|
},
|
|
1680
1766
|
/**
|
|
1681
|
-
* Seats added by phone before this person existed become theirs
|
|
1767
|
+
* Seats added by phone before this person existed become invitations to them: theirs to accept
|
|
1768
|
+
* or decline, like any other.
|
|
1682
1769
|
* @param {Object} person
|
|
1683
1770
|
* @returns {Promise<number>} seats claimed
|
|
1684
1771
|
*/
|
|
@@ -1689,10 +1776,24 @@ const person = ({ strapi }) => ({
|
|
|
1689
1776
|
select: ["id"]
|
|
1690
1777
|
});
|
|
1691
1778
|
for (const seat of seats) {
|
|
1692
|
-
await strapi.db.query(UID.MEMBER).update({ where: { id: seat.id }, data: { person: person2.id
|
|
1779
|
+
await strapi.db.query(UID.MEMBER).update({ where: { id: seat.id }, data: { person: person2.id } });
|
|
1693
1780
|
}
|
|
1694
1781
|
return seats.length;
|
|
1695
1782
|
},
|
|
1783
|
+
/**
|
|
1784
|
+
* What a person chose for themselves.
|
|
1785
|
+
* @param {Object} person
|
|
1786
|
+
* @param {{ autoAcceptInvites?: boolean }} input
|
|
1787
|
+
* @returns {Promise<Object>} the person, updated
|
|
1788
|
+
*/
|
|
1789
|
+
async setPreferences(person2, input) {
|
|
1790
|
+
if (typeof input?.autoAcceptInvites !== "boolean")
|
|
1791
|
+
throw badRequest(ERROR_CODES.VALIDATION, "Préférence invalide.");
|
|
1792
|
+
return strapi.db.query(UID.PERSON).update({
|
|
1793
|
+
where: { id: person2.id },
|
|
1794
|
+
data: { autoAcceptInvites: input.autoAcceptInvites }
|
|
1795
|
+
});
|
|
1796
|
+
},
|
|
1696
1797
|
/** The registered person with this phone, if any. */
|
|
1697
1798
|
async byPhone(phone) {
|
|
1698
1799
|
return phone ? strapi.db.query(UID.PERSON).findOne({ where: { phone } }) : null;
|
|
@@ -1847,6 +1948,7 @@ const ledger = ({ strapi }) => ({
|
|
|
1847
1948
|
return books.settlements.filter((s) => s.status === SETTLEMENT_STATUS.PENDING && s.from === from && s.to === to).reduce((sum, s) => sum + s.amount, 0);
|
|
1848
1949
|
}
|
|
1849
1950
|
});
|
|
1951
|
+
const HELD = Object.freeze({ leftAt: { $null: true }, joinedAt: { $notNull: true } });
|
|
1850
1952
|
const cleanName = (raw) => String(raw ?? "").trim().replace(/\s+/g, " ").slice(0, NAME_MAX);
|
|
1851
1953
|
const group = ({ strapi }) => {
|
|
1852
1954
|
const svc = (name) => strapi.plugin(PLUGIN_ID).service(name);
|
|
@@ -1873,7 +1975,7 @@ const group = ({ strapi }) => {
|
|
|
1873
1975
|
async seat(person2, documentId, { writable = false } = {}) {
|
|
1874
1976
|
const group2 = await groups().findOne({ where: { documentId: String(documentId) } });
|
|
1875
1977
|
const me = group2 && await members().findOne({
|
|
1876
|
-
where: { group: group2.id, person: person2.id,
|
|
1978
|
+
where: { group: group2.id, person: person2.id, ...HELD },
|
|
1877
1979
|
populate: { person: true }
|
|
1878
1980
|
});
|
|
1879
1981
|
if (!me) throw notFound(ERROR_CODES.GROUP_NOT_FOUND, "Groupe introuvable.");
|
|
@@ -1889,7 +1991,7 @@ const group = ({ strapi }) => {
|
|
|
1889
1991
|
/** The seats a person holds, groups populated; archived groups only on request. */
|
|
1890
1992
|
async seatsOf(person2, { archived = false } = {}) {
|
|
1891
1993
|
const rows = await members().findMany({
|
|
1892
|
-
where: { person: person2.id,
|
|
1994
|
+
where: { person: person2.id, ...HELD },
|
|
1893
1995
|
populate: { group: true, person: true }
|
|
1894
1996
|
});
|
|
1895
1997
|
return rows.filter((m) => m.group && Boolean(m.group.archivedAt) === archived);
|
|
@@ -1906,7 +2008,7 @@ const group = ({ strapi }) => {
|
|
|
1906
2008
|
const name = cleanName(input.name);
|
|
1907
2009
|
if (!name) throw badRequest(ERROR_CODES.VALIDATION, "Donne un nom au groupe.");
|
|
1908
2010
|
const limits = svc("settings").limits();
|
|
1909
|
-
const held = await members().count({ where: { person: person2.id,
|
|
2011
|
+
const held = await members().count({ where: { person: person2.id, ...HELD } });
|
|
1910
2012
|
if (held >= limits.maxGroupsPerPerson)
|
|
1911
2013
|
throw unprocessable(
|
|
1912
2014
|
ERROR_CODES.GROUP_LIMIT,
|
|
@@ -1934,7 +2036,7 @@ const group = ({ strapi }) => {
|
|
|
1934
2036
|
joinedAt: now
|
|
1935
2037
|
}
|
|
1936
2038
|
});
|
|
1937
|
-
for (const other of others) await svc("member").seatFor(g, other);
|
|
2039
|
+
for (const other of others) await svc("member").seatFor(g, other, person2);
|
|
1938
2040
|
return g;
|
|
1939
2041
|
});
|
|
1940
2042
|
await svc("audit").log({
|
|
@@ -1969,8 +2071,8 @@ const group = ({ strapi }) => {
|
|
|
1969
2071
|
return updated;
|
|
1970
2072
|
},
|
|
1971
2073
|
/**
|
|
1972
|
-
* Take a seat with an invite code.
|
|
1973
|
-
*
|
|
2074
|
+
* Take a seat with an invite code. Entering the code of a group one was invited to accepts the
|
|
2075
|
+
* invitation; joining twice is not an error.
|
|
1974
2076
|
*/
|
|
1975
2077
|
async join(person2, rawCode) {
|
|
1976
2078
|
const inviteCode = String(rawCode ?? "").trim().toUpperCase();
|
|
@@ -1980,6 +2082,7 @@ const group = ({ strapi }) => {
|
|
|
1980
2082
|
const existing = await members().findOne({
|
|
1981
2083
|
where: { group: group2.id, person: person2.id, leftAt: { $null: true } }
|
|
1982
2084
|
});
|
|
2085
|
+
if (existing && !existing.joinedAt) await svc("member").take({ ...existing, group: group2 }, person2);
|
|
1983
2086
|
if (existing) return group2;
|
|
1984
2087
|
const limits = svc("settings").limits();
|
|
1985
2088
|
const count = await members().count({ where: { group: group2.id, leftAt: { $null: true } } });
|
|
@@ -2052,8 +2155,14 @@ const member = ({ strapi }) => {
|
|
|
2052
2155
|
throw conflict(ERROR_CODES.MEMBER_EXISTS, "Un numéro apparaît deux fois.");
|
|
2053
2156
|
return parsed;
|
|
2054
2157
|
},
|
|
2055
|
-
/**
|
|
2056
|
-
|
|
2158
|
+
/**
|
|
2159
|
+
* Create the seat. When that phone already uses the app it is an invitation to them, taken at
|
|
2160
|
+
* once only for someone who accepts every invitation.
|
|
2161
|
+
* @param {Object} group
|
|
2162
|
+
* @param {{ displayName: string, phone: string|null }} input
|
|
2163
|
+
* @param {Object} inviter the person adding them
|
|
2164
|
+
*/
|
|
2165
|
+
async seatFor(group2, { displayName, phone }, inviter) {
|
|
2057
2166
|
const person2 = await svc("person").byPhone(phone);
|
|
2058
2167
|
return members().create({
|
|
2059
2168
|
data: {
|
|
@@ -2062,10 +2171,20 @@ const member = ({ strapi }) => {
|
|
|
2062
2171
|
displayName,
|
|
2063
2172
|
phone,
|
|
2064
2173
|
role: ROLE.MEMBER,
|
|
2065
|
-
|
|
2174
|
+
invitedBy: inviter?.id ?? null,
|
|
2175
|
+
joinedAt: person2?.autoAcceptInvites ? /* @__PURE__ */ new Date() : null
|
|
2066
2176
|
}
|
|
2067
2177
|
});
|
|
2068
2178
|
},
|
|
2179
|
+
/** Is someone sitting here: a person who is in the group, not a name or an open invitation? */
|
|
2180
|
+
isHeld(seat) {
|
|
2181
|
+
return Boolean(seat.person) && Boolean(seat.joinedAt);
|
|
2182
|
+
},
|
|
2183
|
+
/** Where an invitation stands, or null for a seat that is held or was never anyone's. */
|
|
2184
|
+
inviteOf(seat) {
|
|
2185
|
+
if (seat.declinedAt) return INVITE.DECLINED;
|
|
2186
|
+
return seat.person && !seat.joinedAt ? INVITE.PENDING : null;
|
|
2187
|
+
},
|
|
2069
2188
|
/**
|
|
2070
2189
|
* @param {{ group: Object, me: Object }} seat the caller's
|
|
2071
2190
|
* @param {{ name: string, phone?: string }} raw
|
|
@@ -2086,7 +2205,7 @@ const member = ({ strapi }) => {
|
|
|
2086
2205
|
const format2 = svc("format");
|
|
2087
2206
|
if (input.phone && seats.some((s) => format2.phoneOf(s) === input.phone))
|
|
2088
2207
|
throw conflict(ERROR_CODES.MEMBER_EXISTS, "Ce numéro est déjà dans le groupe.");
|
|
2089
|
-
const created = await this.seatFor(group2, input);
|
|
2208
|
+
const created = await this.seatFor(group2, input, me.person);
|
|
2090
2209
|
await svc("audit").log({
|
|
2091
2210
|
group: group2,
|
|
2092
2211
|
actor: me.person,
|
|
@@ -2104,17 +2223,23 @@ const member = ({ strapi }) => {
|
|
|
2104
2223
|
if (!seat) throw notFound(ERROR_CODES.MEMBER_NOT_FOUND, "Membre introuvable.");
|
|
2105
2224
|
return seat;
|
|
2106
2225
|
},
|
|
2107
|
-
/**
|
|
2108
|
-
async
|
|
2226
|
+
/** What a seat still has on the books: its balance, and whether a payment awaits an answer. */
|
|
2227
|
+
async standing(group2, seat) {
|
|
2109
2228
|
const books = await svc("ledger").of(group2);
|
|
2110
2229
|
const busy = books.settlements.some(
|
|
2111
2230
|
(s) => s.status === SETTLEMENT_STATUS.PENDING && [s.from, s.to].includes(seat.id)
|
|
2112
2231
|
);
|
|
2113
|
-
|
|
2232
|
+
const balance = books.balanceOf(seat.id);
|
|
2233
|
+
return { balance, settled: balance === 0 && !busy };
|
|
2234
|
+
},
|
|
2235
|
+
/** A seat is only vacated once it is even and nothing awaits confirmation on it. */
|
|
2236
|
+
async assertSettled(group2, seat) {
|
|
2237
|
+
const { balance, settled } = await this.standing(group2, seat);
|
|
2238
|
+
if (!settled)
|
|
2114
2239
|
throw unprocessable(
|
|
2115
2240
|
ERROR_CODES.MEMBER_NOT_SETTLED,
|
|
2116
2241
|
"Les comptes de ce membre ne sont pas à zéro.",
|
|
2117
|
-
{ balance
|
|
2242
|
+
{ balance }
|
|
2118
2243
|
);
|
|
2119
2244
|
},
|
|
2120
2245
|
/** The owner removes someone else. */
|
|
@@ -2148,6 +2273,78 @@ const member = ({ strapi }) => {
|
|
|
2148
2273
|
/** Is this seat held by this person? */
|
|
2149
2274
|
isHeldBy(seat, person2) {
|
|
2150
2275
|
return idOf(seat.person) === person2.id;
|
|
2276
|
+
},
|
|
2277
|
+
/** The invitations a person has not answered, newest first; archived groups are not offered. */
|
|
2278
|
+
async invitations(person2) {
|
|
2279
|
+
const rows = await members().findMany({
|
|
2280
|
+
where: { person: person2.id, joinedAt: { $null: true }, leftAt: { $null: true } },
|
|
2281
|
+
populate: { group: true, invitedBy: true },
|
|
2282
|
+
orderBy: { id: "desc" }
|
|
2283
|
+
});
|
|
2284
|
+
const open = rows.filter((seat) => seat.group && !seat.group.archivedAt);
|
|
2285
|
+
return Promise.all(
|
|
2286
|
+
open.map(async (seat) => ({
|
|
2287
|
+
...seat,
|
|
2288
|
+
memberCount: await members().count({
|
|
2289
|
+
where: { group: seat.group.id, leftAt: { $null: true } }
|
|
2290
|
+
})
|
|
2291
|
+
}))
|
|
2292
|
+
);
|
|
2293
|
+
},
|
|
2294
|
+
/** One open invitation of this person, by the seat's document id. */
|
|
2295
|
+
async invitation(person2, documentId) {
|
|
2296
|
+
const seat = await members().findOne({
|
|
2297
|
+
where: {
|
|
2298
|
+
documentId: String(documentId),
|
|
2299
|
+
person: person2.id,
|
|
2300
|
+
joinedAt: { $null: true },
|
|
2301
|
+
leftAt: { $null: true }
|
|
2302
|
+
},
|
|
2303
|
+
populate: { group: true, person: true }
|
|
2304
|
+
});
|
|
2305
|
+
if (!seat?.group)
|
|
2306
|
+
throw notFound(ERROR_CODES.INVITATION_NOT_FOUND, "Cette invitation n'existe plus.");
|
|
2307
|
+
return seat;
|
|
2308
|
+
},
|
|
2309
|
+
/** The person takes the seat they were offered. */
|
|
2310
|
+
async take(seat, person2) {
|
|
2311
|
+
await members().update({ where: { id: seat.id }, data: { joinedAt: /* @__PURE__ */ new Date() } });
|
|
2312
|
+
await svc("audit").log({
|
|
2313
|
+
group: seat.group,
|
|
2314
|
+
actor: person2,
|
|
2315
|
+
action: EVENT.MEMBER_JOINED,
|
|
2316
|
+
targetId: seat.documentId
|
|
2317
|
+
});
|
|
2318
|
+
},
|
|
2319
|
+
/**
|
|
2320
|
+
* @param {Object} person
|
|
2321
|
+
* @param {string} documentId the invitation's
|
|
2322
|
+
* @returns {Promise<Object>} the group they are now in
|
|
2323
|
+
*/
|
|
2324
|
+
async accept(person2, documentId) {
|
|
2325
|
+
const seat = await this.invitation(person2, documentId);
|
|
2326
|
+
await this.take(seat, person2);
|
|
2327
|
+
return seat.group;
|
|
2328
|
+
},
|
|
2329
|
+
/**
|
|
2330
|
+
* The person says no. A seat with nothing on the books goes; one the group already counted in
|
|
2331
|
+
* its expenses stays as a name nobody holds, so the books still add up.
|
|
2332
|
+
* @param {Object} person
|
|
2333
|
+
* @param {string} documentId the invitation's
|
|
2334
|
+
*/
|
|
2335
|
+
async decline(person2, documentId) {
|
|
2336
|
+
const seat = await this.invitation(person2, documentId);
|
|
2337
|
+
const now = /* @__PURE__ */ new Date();
|
|
2338
|
+
const { settled } = await this.standing(seat.group, seat);
|
|
2339
|
+
const data = settled ? { leftAt: now, declinedAt: now } : { person: null, declinedAt: now };
|
|
2340
|
+
await members().update({ where: { id: seat.id }, data });
|
|
2341
|
+
await svc("audit").log({
|
|
2342
|
+
group: seat.group,
|
|
2343
|
+
actor: person2,
|
|
2344
|
+
action: EVENT.MEMBER_DECLINED,
|
|
2345
|
+
targetId: seat.documentId,
|
|
2346
|
+
data: { kept: !settled }
|
|
2347
|
+
});
|
|
2151
2348
|
}
|
|
2152
2349
|
};
|
|
2153
2350
|
};
|
|
@@ -2394,7 +2591,7 @@ const settlement = ({ strapi }) => {
|
|
|
2394
2591
|
to,
|
|
2395
2592
|
received,
|
|
2396
2593
|
method: String(input.method),
|
|
2397
|
-
instant: received || method.instant || !to
|
|
2594
|
+
instant: received || method.instant || !svc("member").isHeld(to),
|
|
2398
2595
|
reference: text(input?.reference, REF_MAX),
|
|
2399
2596
|
note: text(input?.note, NOTE_MAX),
|
|
2400
2597
|
clientRef: text(input?.clientRef, REF_MAX)
|
|
@@ -2587,8 +2784,10 @@ const orderTotals = ({ items, lines, deliveryFee = 0 }) => {
|
|
|
2587
2784
|
};
|
|
2588
2785
|
const LINE_POPULATE = { item: { select: ["id"] }, member: { select: ["id"] } };
|
|
2589
2786
|
const ORDER_POPULATE = { organizer: { select: ["id"] } };
|
|
2787
|
+
const CURRENT_POPULATE = { group: { select: ["id"] }, organizer: { populate: { person: true } } };
|
|
2590
2788
|
const iso = (date) => date ? new Date(date).toISOString() : null;
|
|
2591
|
-
const
|
|
2789
|
+
const tidy = (raw, max) => String(raw ?? "").trim().replace(/\s+/g, " ").slice(0, max);
|
|
2790
|
+
const label = (raw) => tidy(raw, LABEL_MAX);
|
|
2592
2791
|
const invalid = (message, details) => badRequest(ERROR_CODES.VALIDATION, message, details);
|
|
2593
2792
|
const expenseRefOf = (order2) => `order:${order2.documentId}`;
|
|
2594
2793
|
const order = ({ strapi }) => {
|
|
@@ -2674,7 +2873,8 @@ const order = ({ strapi }) => {
|
|
|
2674
2873
|
lines: taken.map((l) => ({
|
|
2675
2874
|
item: idOf(l.item),
|
|
2676
2875
|
member: idOf(l.member),
|
|
2677
|
-
quantity: l.quantity
|
|
2876
|
+
quantity: l.quantity,
|
|
2877
|
+
note: l.note || null
|
|
2678
2878
|
}))
|
|
2679
2879
|
};
|
|
2680
2880
|
},
|
|
@@ -2691,7 +2891,11 @@ const order = ({ strapi }) => {
|
|
|
2691
2891
|
status: order2.status
|
|
2692
2892
|
});
|
|
2693
2893
|
},
|
|
2694
|
-
/**
|
|
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
|
+
*/
|
|
2695
2899
|
quantities(input, offered) {
|
|
2696
2900
|
const byDoc = new Map(offered.map((item) => [item.documentId, item.id]));
|
|
2697
2901
|
const list = Array.isArray(input?.lines) ? input.lines : null;
|
|
@@ -2705,7 +2909,7 @@ const order = ({ strapi }) => {
|
|
|
2705
2909
|
throw invalid(`Une quantité va de 0 à ${ORDER_MAX_QUANTITY}.`, {
|
|
2706
2910
|
max: ORDER_MAX_QUANTITY
|
|
2707
2911
|
});
|
|
2708
|
-
wanted.set(item, quantity);
|
|
2912
|
+
wanted.set(item, { quantity, note: tidy(raw?.note, ORDER_NOTE_MAX) || null });
|
|
2709
2913
|
}
|
|
2710
2914
|
return wanted;
|
|
2711
2915
|
},
|
|
@@ -2714,7 +2918,7 @@ const order = ({ strapi }) => {
|
|
|
2714
2918
|
* (someone who told them, someone without the app), and still can after the closing time.
|
|
2715
2919
|
* @param {{ group: Object, me: Object }} seat the caller's
|
|
2716
2920
|
* @param {string} documentId the order
|
|
2717
|
-
* @param {{ member?: string, lines: Array<{ item: string, quantity: number }> }} input
|
|
2921
|
+
* @param {{ member?: string, lines: Array<{ item: string, quantity: number, note?: string }> }} input
|
|
2718
2922
|
*/
|
|
2719
2923
|
async setLines({ group: group2, me }, documentId, input) {
|
|
2720
2924
|
const order2 = await this.find(group2, documentId);
|
|
@@ -2731,10 +2935,10 @@ const order = ({ strapi }) => {
|
|
|
2731
2935
|
const wanted = this.quantities(input, offered);
|
|
2732
2936
|
await strapi.db.transaction(async () => {
|
|
2733
2937
|
await lines().deleteMany({ where: { order: order2.id, member: target.id } });
|
|
2734
|
-
for (const [item, quantity] of wanted)
|
|
2938
|
+
for (const [item, { quantity, note }] of wanted)
|
|
2735
2939
|
if (quantity > 0)
|
|
2736
2940
|
await lines().create({
|
|
2737
|
-
data: { order: order2.id, item, member: target.id, quantity }
|
|
2941
|
+
data: { order: order2.id, item, member: target.id, quantity, note }
|
|
2738
2942
|
});
|
|
2739
2943
|
});
|
|
2740
2944
|
return order2;
|
|
@@ -2849,7 +3053,7 @@ const order = ({ strapi }) => {
|
|
|
2849
3053
|
yours
|
|
2850
3054
|
};
|
|
2851
3055
|
if (!detailed) return summary;
|
|
2852
|
-
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 }));
|
|
2853
3057
|
const quantityOf = (item, member2) => taken.filter((l) => l.item === item && (member2 == null || l.member === member2)).reduce((sum, l) => sum + l.quantity, 0);
|
|
2854
3058
|
return {
|
|
2855
3059
|
...summary,
|
|
@@ -2867,6 +3071,60 @@ const order = ({ strapi }) => {
|
|
|
2867
3071
|
}))
|
|
2868
3072
|
};
|
|
2869
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
|
+
},
|
|
2870
3128
|
/** The group's orders as a member reads them: the open ones first, then the latest. */
|
|
2871
3129
|
async list(seat, viewer) {
|
|
2872
3130
|
const rows = await orders().findMany({
|
package/dist/server/index.mjs
CHANGED
|
@@ -28,6 +28,12 @@ const RBAC = Object.freeze({
|
|
|
28
28
|
});
|
|
29
29
|
const SPLIT_MODE = Object.freeze({ EQUAL: "equal", WEIGHTED: "weighted", FREE: "free" });
|
|
30
30
|
const ROLE = Object.freeze({ OWNER: "owner", MEMBER: "member" });
|
|
31
|
+
const INVITE = Object.freeze({
|
|
32
|
+
/** Added by a member; the person has not said yes or no. */
|
|
33
|
+
PENDING: "pending",
|
|
34
|
+
/** The person said no, and the seat stays as a name because it is on the books. */
|
|
35
|
+
DECLINED: "declined"
|
|
36
|
+
});
|
|
31
37
|
const SETTLEMENT_STATUS = Object.freeze({
|
|
32
38
|
/** Declared; the receiver has not confirmed yet. Does not move balances. */
|
|
33
39
|
PENDING: "pending",
|
|
@@ -49,6 +55,7 @@ const EVENT = Object.freeze({
|
|
|
49
55
|
GROUP_ARCHIVED: "group.archived",
|
|
50
56
|
MEMBER_ADDED: "member.added",
|
|
51
57
|
MEMBER_JOINED: "member.joined",
|
|
58
|
+
MEMBER_DECLINED: "member.declined",
|
|
52
59
|
MEMBER_LEFT: "member.left",
|
|
53
60
|
MEMBER_REMOVED: "member.removed",
|
|
54
61
|
EXPENSE_CREATED: "expense.created",
|
|
@@ -73,6 +80,8 @@ const ORDER_STATUS = Object.freeze({
|
|
|
73
80
|
const ORDER_MAX_ITEMS = 30;
|
|
74
81
|
const ORDER_MAX_QUANTITY = 99;
|
|
75
82
|
const ORDER_LIST_SIZE = 20;
|
|
83
|
+
const ORDER_NOTE_MAX = 80;
|
|
84
|
+
const ORDER_RECENT_MS = 24 * 60 * 60 * 1e3;
|
|
76
85
|
const DEFAULT_LIMITS = Object.freeze({
|
|
77
86
|
maxGroupsPerPerson: 50,
|
|
78
87
|
maxMembersPerGroup: 50,
|
|
@@ -105,6 +114,7 @@ const ERROR_CODES = Object.freeze({
|
|
|
105
114
|
NOT_A_MEMBER: "NOT_A_MEMBER",
|
|
106
115
|
NOT_ALLOWED: "NOT_ALLOWED",
|
|
107
116
|
INVITE_INVALID: "INVITE_INVALID",
|
|
117
|
+
INVITATION_NOT_FOUND: "INVITATION_NOT_FOUND",
|
|
108
118
|
MEMBER_NOT_FOUND: "MEMBER_NOT_FOUND",
|
|
109
119
|
MEMBER_EXISTS: "MEMBER_EXISTS",
|
|
110
120
|
MEMBER_LIMIT: "MEMBER_LIMIT",
|
|
@@ -273,6 +283,10 @@ const attributes$9 = {
|
|
|
273
283
|
lastActiveAt: {
|
|
274
284
|
type: "datetime"
|
|
275
285
|
},
|
|
286
|
+
autoAcceptInvites: {
|
|
287
|
+
type: "boolean",
|
|
288
|
+
"default": false
|
|
289
|
+
},
|
|
276
290
|
memberships: {
|
|
277
291
|
type: "relation",
|
|
278
292
|
relation: "oneToMany",
|
|
@@ -353,7 +367,7 @@ const info$7 = {
|
|
|
353
367
|
singularName: "member",
|
|
354
368
|
pluralName: "members",
|
|
355
369
|
displayName: "Split Member",
|
|
356
|
-
description: "A seat in a group: a registered person, or someone a member added by name (and maybe phone) who has not opened the app yet."
|
|
370
|
+
description: "A seat in a group: a registered person who joined or was invited and has not answered yet, or someone a member added by name (and maybe phone) who has not opened the app yet."
|
|
357
371
|
};
|
|
358
372
|
const options$7 = {
|
|
359
373
|
draftAndPublish: false
|
|
@@ -403,6 +417,14 @@ const attributes$7 = {
|
|
|
403
417
|
},
|
|
404
418
|
leftAt: {
|
|
405
419
|
type: "datetime"
|
|
420
|
+
},
|
|
421
|
+
invitedBy: {
|
|
422
|
+
type: "relation",
|
|
423
|
+
relation: "manyToOne",
|
|
424
|
+
target: "plugin::split.person"
|
|
425
|
+
},
|
|
426
|
+
declinedAt: {
|
|
427
|
+
type: "datetime"
|
|
406
428
|
}
|
|
407
429
|
};
|
|
408
430
|
const schema$7 = {
|
|
@@ -823,7 +845,7 @@ const info = {
|
|
|
823
845
|
singularName: "order-line",
|
|
824
846
|
pluralName: "order-lines",
|
|
825
847
|
displayName: "Split Order Line",
|
|
826
|
-
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."
|
|
827
849
|
};
|
|
828
850
|
const options = {
|
|
829
851
|
draftAndPublish: false
|
|
@@ -856,6 +878,10 @@ const attributes = {
|
|
|
856
878
|
type: "integer",
|
|
857
879
|
required: true,
|
|
858
880
|
min: 1
|
|
881
|
+
},
|
|
882
|
+
note: {
|
|
883
|
+
type: "string",
|
|
884
|
+
maxLength: 80
|
|
859
885
|
}
|
|
860
886
|
};
|
|
861
887
|
const schema = {
|
|
@@ -956,12 +982,33 @@ const app = ({ strapi }) => {
|
|
|
956
982
|
me: handle(strapi, async (ctx) => {
|
|
957
983
|
ok(ctx, svc("format").person(viewer(ctx), Boolean(ctx.state.splitPersonIsNew)));
|
|
958
984
|
}),
|
|
985
|
+
updateMe: handle(strapi, async (ctx) => {
|
|
986
|
+
const person2 = await svc("person").setPreferences(viewer(ctx), body(ctx));
|
|
987
|
+
ok(ctx, svc("format").person(person2));
|
|
988
|
+
}),
|
|
959
989
|
overview: handle(strapi, async (ctx) => ok(ctx, await svc("view").overview(viewer(ctx)))),
|
|
960
990
|
activity: handle(strapi, async (ctx) => {
|
|
961
991
|
const page = parsePagination(ctx.query);
|
|
962
992
|
const { items, total } = await svc("view").activity(viewer(ctx), page);
|
|
963
993
|
ctx.body = paged(items, total, page);
|
|
964
994
|
}),
|
|
995
|
+
// ── Invitations ──────────────────────────────────────────────────────
|
|
996
|
+
invitations: handle(strapi, async (ctx) => {
|
|
997
|
+
const seats = await svc("member").invitations(viewer(ctx));
|
|
998
|
+
ok(
|
|
999
|
+
ctx,
|
|
1000
|
+
seats.map((s) => svc("format").invitation(s))
|
|
1001
|
+
);
|
|
1002
|
+
}),
|
|
1003
|
+
acceptInvitation: handle(strapi, async (ctx) => {
|
|
1004
|
+
const group2 = await svc("member").accept(viewer(ctx), ctx.params.invitation);
|
|
1005
|
+
const mine = await svc("group").seat(viewer(ctx), group2.documentId);
|
|
1006
|
+
ok(ctx, await svc("view").group(mine, viewer(ctx)));
|
|
1007
|
+
}),
|
|
1008
|
+
declineInvitation: handle(strapi, async (ctx) => {
|
|
1009
|
+
await svc("member").decline(viewer(ctx), ctx.params.invitation);
|
|
1010
|
+
noContent(ctx);
|
|
1011
|
+
}),
|
|
965
1012
|
// ── Groups ───────────────────────────────────────────────────────────
|
|
966
1013
|
createGroup: handle(strapi, async (ctx) => {
|
|
967
1014
|
const group2 = await svc("group").create(viewer(ctx), body(ctx));
|
|
@@ -1052,6 +1099,7 @@ const app = ({ strapi }) => {
|
|
|
1052
1099
|
ok(ctx, svc("format").settlement(settlement2, mine.me, viewer(ctx)));
|
|
1053
1100
|
}),
|
|
1054
1101
|
// ── Orders ───────────────────────────────────────────────────────────
|
|
1102
|
+
currentOrders: handle(strapi, async (ctx) => ok(ctx, await svc("order").current(viewer(ctx)))),
|
|
1055
1103
|
orders: handle(
|
|
1056
1104
|
strapi,
|
|
1057
1105
|
async (ctx) => ok(ctx, await svc("order").list(await seat(ctx), viewer(ctx)))
|
|
@@ -1193,11 +1241,17 @@ const view = ({ strapi }) => {
|
|
|
1193
1241
|
const debts = perGroup.flatMap((g) => g.debts).sort((a, b) => b.amount - a.amount);
|
|
1194
1242
|
const credits = perGroup.flatMap((g) => g.credits).sort((a, b) => b.amount - a.amount);
|
|
1195
1243
|
const sum = (list) => list.reduce((total, item) => total + item.amount, 0);
|
|
1244
|
+
const [invitations, orders] = await Promise.all([
|
|
1245
|
+
svc("member").invitations(viewer),
|
|
1246
|
+
svc("order").current(viewer)
|
|
1247
|
+
]);
|
|
1196
1248
|
return {
|
|
1197
1249
|
owe: sum(debts),
|
|
1198
1250
|
owed: sum(credits),
|
|
1199
1251
|
debts,
|
|
1200
1252
|
credits,
|
|
1253
|
+
invitations: invitations.map((seat) => format2.invitation(seat)),
|
|
1254
|
+
orders,
|
|
1201
1255
|
groups: perGroup.map((g) => g.summary).sort((a, b) => new Date(b.lastActivityAt ?? 0) - new Date(a.lastActivityAt ?? 0))
|
|
1202
1256
|
};
|
|
1203
1257
|
},
|
|
@@ -1404,8 +1458,14 @@ const route$1 = (method, path, handler) => ({
|
|
|
1404
1458
|
const contentApi = [
|
|
1405
1459
|
// person
|
|
1406
1460
|
route$1("GET", "/me", "me"),
|
|
1461
|
+
// PUT, not PATCH: some app runtimes (mini-programs) cannot send PATCH.
|
|
1462
|
+
route$1("PUT", "/me", "updateMe"),
|
|
1407
1463
|
route$1("GET", "/overview", "overview"),
|
|
1408
1464
|
route$1("GET", "/activity", "activity"),
|
|
1465
|
+
// invitations: being added to a group is an offer, answered by the person
|
|
1466
|
+
route$1("GET", "/invitations", "invitations"),
|
|
1467
|
+
route$1("POST", "/invitations/:invitation/accept", "acceptInvitation"),
|
|
1468
|
+
route$1("POST", "/invitations/:invitation/decline", "declineInvitation"),
|
|
1409
1469
|
// groups
|
|
1410
1470
|
route$1("POST", "/groups", "createGroup"),
|
|
1411
1471
|
route$1("POST", "/groups/join", "joinGroup"),
|
|
@@ -1430,6 +1490,7 @@ const contentApi = [
|
|
|
1430
1490
|
route$1("POST", "/groups/:group/settlements/:settlement/reject", "rejectSettlement"),
|
|
1431
1491
|
route$1("POST", "/groups/:group/settlements/:settlement/cancel", "cancelSettlement"),
|
|
1432
1492
|
// orders: priced items the members order from; closing one writes an expense
|
|
1493
|
+
route$1("GET", "/orders", "currentOrders"),
|
|
1433
1494
|
route$1("GET", "/groups/:group/orders", "orders"),
|
|
1434
1495
|
route$1("POST", "/groups/:group/orders", "createOrder"),
|
|
1435
1496
|
route$1("GET", "/groups/:group/orders/:order", "order"),
|
|
@@ -1540,7 +1601,13 @@ const audit = ({ strapi }) => ({
|
|
|
1540
1601
|
const iso$1 = (date) => date ? new Date(date).toISOString() : null;
|
|
1541
1602
|
const format = () => ({
|
|
1542
1603
|
person(p, isNew = false) {
|
|
1543
|
-
return {
|
|
1604
|
+
return {
|
|
1605
|
+
id: p.documentId,
|
|
1606
|
+
displayName: p.displayName ?? null,
|
|
1607
|
+
phone: p.phone ?? null,
|
|
1608
|
+
autoAcceptInvites: Boolean(p.autoAcceptInvites),
|
|
1609
|
+
isNew
|
|
1610
|
+
};
|
|
1544
1611
|
},
|
|
1545
1612
|
/** The name a seat goes by: the person's own once they have one, the name it was added under otherwise. */
|
|
1546
1613
|
nameOf(member2) {
|
|
@@ -1560,10 +1627,29 @@ const format = () => ({
|
|
|
1560
1627
|
displayName: this.nameOf(m),
|
|
1561
1628
|
isYou: Boolean(viewer) && idOf(m.person) === viewer.id,
|
|
1562
1629
|
isRegistered: Boolean(m.person),
|
|
1630
|
+
invite: this.inviteOf(m),
|
|
1563
1631
|
role: m.role,
|
|
1564
1632
|
hasLeft: Boolean(m.leftAt)
|
|
1565
1633
|
};
|
|
1566
1634
|
},
|
|
1635
|
+
/** `pending` while an invited person has not answered, `declined` once they said no, else null. */
|
|
1636
|
+
inviteOf(m) {
|
|
1637
|
+
if (m.declinedAt) return INVITE.DECLINED;
|
|
1638
|
+
return m.person && !m.joinedAt ? INVITE.PENDING : null;
|
|
1639
|
+
},
|
|
1640
|
+
/**
|
|
1641
|
+
* An invitation as the invited person sees it: enough to decide, nothing of the books.
|
|
1642
|
+
* @param {Object} seat member row, `group` and `invitedBy` populated, with a `memberCount`
|
|
1643
|
+
*/
|
|
1644
|
+
invitation(seat) {
|
|
1645
|
+
return {
|
|
1646
|
+
id: seat.documentId,
|
|
1647
|
+
group: { id: seat.group.documentId, name: seat.group.name },
|
|
1648
|
+
invitedBy: seat.invitedBy ? { displayName: seat.invitedBy.displayName ?? null } : null,
|
|
1649
|
+
memberCount: seat.memberCount,
|
|
1650
|
+
invitedAt: iso$1(seat.createdAt)
|
|
1651
|
+
};
|
|
1652
|
+
},
|
|
1567
1653
|
/** A seat as an administrator sees it: the phone masked. */
|
|
1568
1654
|
adminMember(m) {
|
|
1569
1655
|
return { ...this.member(m), phone: maskPhone(this.phoneOf(m)), joinedAt: iso$1(m.joinedAt) };
|
|
@@ -1677,7 +1763,8 @@ const person = ({ strapi }) => ({
|
|
|
1677
1763
|
}
|
|
1678
1764
|
},
|
|
1679
1765
|
/**
|
|
1680
|
-
* Seats added by phone before this person existed become theirs
|
|
1766
|
+
* Seats added by phone before this person existed become invitations to them: theirs to accept
|
|
1767
|
+
* or decline, like any other.
|
|
1681
1768
|
* @param {Object} person
|
|
1682
1769
|
* @returns {Promise<number>} seats claimed
|
|
1683
1770
|
*/
|
|
@@ -1688,10 +1775,24 @@ const person = ({ strapi }) => ({
|
|
|
1688
1775
|
select: ["id"]
|
|
1689
1776
|
});
|
|
1690
1777
|
for (const seat of seats) {
|
|
1691
|
-
await strapi.db.query(UID.MEMBER).update({ where: { id: seat.id }, data: { person: person2.id
|
|
1778
|
+
await strapi.db.query(UID.MEMBER).update({ where: { id: seat.id }, data: { person: person2.id } });
|
|
1692
1779
|
}
|
|
1693
1780
|
return seats.length;
|
|
1694
1781
|
},
|
|
1782
|
+
/**
|
|
1783
|
+
* What a person chose for themselves.
|
|
1784
|
+
* @param {Object} person
|
|
1785
|
+
* @param {{ autoAcceptInvites?: boolean }} input
|
|
1786
|
+
* @returns {Promise<Object>} the person, updated
|
|
1787
|
+
*/
|
|
1788
|
+
async setPreferences(person2, input) {
|
|
1789
|
+
if (typeof input?.autoAcceptInvites !== "boolean")
|
|
1790
|
+
throw badRequest(ERROR_CODES.VALIDATION, "Préférence invalide.");
|
|
1791
|
+
return strapi.db.query(UID.PERSON).update({
|
|
1792
|
+
where: { id: person2.id },
|
|
1793
|
+
data: { autoAcceptInvites: input.autoAcceptInvites }
|
|
1794
|
+
});
|
|
1795
|
+
},
|
|
1695
1796
|
/** The registered person with this phone, if any. */
|
|
1696
1797
|
async byPhone(phone) {
|
|
1697
1798
|
return phone ? strapi.db.query(UID.PERSON).findOne({ where: { phone } }) : null;
|
|
@@ -1846,6 +1947,7 @@ const ledger = ({ strapi }) => ({
|
|
|
1846
1947
|
return books.settlements.filter((s) => s.status === SETTLEMENT_STATUS.PENDING && s.from === from && s.to === to).reduce((sum, s) => sum + s.amount, 0);
|
|
1847
1948
|
}
|
|
1848
1949
|
});
|
|
1950
|
+
const HELD = Object.freeze({ leftAt: { $null: true }, joinedAt: { $notNull: true } });
|
|
1849
1951
|
const cleanName = (raw) => String(raw ?? "").trim().replace(/\s+/g, " ").slice(0, NAME_MAX);
|
|
1850
1952
|
const group = ({ strapi }) => {
|
|
1851
1953
|
const svc = (name) => strapi.plugin(PLUGIN_ID).service(name);
|
|
@@ -1872,7 +1974,7 @@ const group = ({ strapi }) => {
|
|
|
1872
1974
|
async seat(person2, documentId, { writable = false } = {}) {
|
|
1873
1975
|
const group2 = await groups().findOne({ where: { documentId: String(documentId) } });
|
|
1874
1976
|
const me = group2 && await members().findOne({
|
|
1875
|
-
where: { group: group2.id, person: person2.id,
|
|
1977
|
+
where: { group: group2.id, person: person2.id, ...HELD },
|
|
1876
1978
|
populate: { person: true }
|
|
1877
1979
|
});
|
|
1878
1980
|
if (!me) throw notFound(ERROR_CODES.GROUP_NOT_FOUND, "Groupe introuvable.");
|
|
@@ -1888,7 +1990,7 @@ const group = ({ strapi }) => {
|
|
|
1888
1990
|
/** The seats a person holds, groups populated; archived groups only on request. */
|
|
1889
1991
|
async seatsOf(person2, { archived = false } = {}) {
|
|
1890
1992
|
const rows = await members().findMany({
|
|
1891
|
-
where: { person: person2.id,
|
|
1993
|
+
where: { person: person2.id, ...HELD },
|
|
1892
1994
|
populate: { group: true, person: true }
|
|
1893
1995
|
});
|
|
1894
1996
|
return rows.filter((m) => m.group && Boolean(m.group.archivedAt) === archived);
|
|
@@ -1905,7 +2007,7 @@ const group = ({ strapi }) => {
|
|
|
1905
2007
|
const name = cleanName(input.name);
|
|
1906
2008
|
if (!name) throw badRequest(ERROR_CODES.VALIDATION, "Donne un nom au groupe.");
|
|
1907
2009
|
const limits = svc("settings").limits();
|
|
1908
|
-
const held = await members().count({ where: { person: person2.id,
|
|
2010
|
+
const held = await members().count({ where: { person: person2.id, ...HELD } });
|
|
1909
2011
|
if (held >= limits.maxGroupsPerPerson)
|
|
1910
2012
|
throw unprocessable(
|
|
1911
2013
|
ERROR_CODES.GROUP_LIMIT,
|
|
@@ -1933,7 +2035,7 @@ const group = ({ strapi }) => {
|
|
|
1933
2035
|
joinedAt: now
|
|
1934
2036
|
}
|
|
1935
2037
|
});
|
|
1936
|
-
for (const other of others) await svc("member").seatFor(g, other);
|
|
2038
|
+
for (const other of others) await svc("member").seatFor(g, other, person2);
|
|
1937
2039
|
return g;
|
|
1938
2040
|
});
|
|
1939
2041
|
await svc("audit").log({
|
|
@@ -1968,8 +2070,8 @@ const group = ({ strapi }) => {
|
|
|
1968
2070
|
return updated;
|
|
1969
2071
|
},
|
|
1970
2072
|
/**
|
|
1971
|
-
* Take a seat with an invite code.
|
|
1972
|
-
*
|
|
2073
|
+
* Take a seat with an invite code. Entering the code of a group one was invited to accepts the
|
|
2074
|
+
* invitation; joining twice is not an error.
|
|
1973
2075
|
*/
|
|
1974
2076
|
async join(person2, rawCode) {
|
|
1975
2077
|
const inviteCode = String(rawCode ?? "").trim().toUpperCase();
|
|
@@ -1979,6 +2081,7 @@ const group = ({ strapi }) => {
|
|
|
1979
2081
|
const existing = await members().findOne({
|
|
1980
2082
|
where: { group: group2.id, person: person2.id, leftAt: { $null: true } }
|
|
1981
2083
|
});
|
|
2084
|
+
if (existing && !existing.joinedAt) await svc("member").take({ ...existing, group: group2 }, person2);
|
|
1982
2085
|
if (existing) return group2;
|
|
1983
2086
|
const limits = svc("settings").limits();
|
|
1984
2087
|
const count = await members().count({ where: { group: group2.id, leftAt: { $null: true } } });
|
|
@@ -2051,8 +2154,14 @@ const member = ({ strapi }) => {
|
|
|
2051
2154
|
throw conflict(ERROR_CODES.MEMBER_EXISTS, "Un numéro apparaît deux fois.");
|
|
2052
2155
|
return parsed;
|
|
2053
2156
|
},
|
|
2054
|
-
/**
|
|
2055
|
-
|
|
2157
|
+
/**
|
|
2158
|
+
* Create the seat. When that phone already uses the app it is an invitation to them, taken at
|
|
2159
|
+
* once only for someone who accepts every invitation.
|
|
2160
|
+
* @param {Object} group
|
|
2161
|
+
* @param {{ displayName: string, phone: string|null }} input
|
|
2162
|
+
* @param {Object} inviter the person adding them
|
|
2163
|
+
*/
|
|
2164
|
+
async seatFor(group2, { displayName, phone }, inviter) {
|
|
2056
2165
|
const person2 = await svc("person").byPhone(phone);
|
|
2057
2166
|
return members().create({
|
|
2058
2167
|
data: {
|
|
@@ -2061,10 +2170,20 @@ const member = ({ strapi }) => {
|
|
|
2061
2170
|
displayName,
|
|
2062
2171
|
phone,
|
|
2063
2172
|
role: ROLE.MEMBER,
|
|
2064
|
-
|
|
2173
|
+
invitedBy: inviter?.id ?? null,
|
|
2174
|
+
joinedAt: person2?.autoAcceptInvites ? /* @__PURE__ */ new Date() : null
|
|
2065
2175
|
}
|
|
2066
2176
|
});
|
|
2067
2177
|
},
|
|
2178
|
+
/** Is someone sitting here: a person who is in the group, not a name or an open invitation? */
|
|
2179
|
+
isHeld(seat) {
|
|
2180
|
+
return Boolean(seat.person) && Boolean(seat.joinedAt);
|
|
2181
|
+
},
|
|
2182
|
+
/** Where an invitation stands, or null for a seat that is held or was never anyone's. */
|
|
2183
|
+
inviteOf(seat) {
|
|
2184
|
+
if (seat.declinedAt) return INVITE.DECLINED;
|
|
2185
|
+
return seat.person && !seat.joinedAt ? INVITE.PENDING : null;
|
|
2186
|
+
},
|
|
2068
2187
|
/**
|
|
2069
2188
|
* @param {{ group: Object, me: Object }} seat the caller's
|
|
2070
2189
|
* @param {{ name: string, phone?: string }} raw
|
|
@@ -2085,7 +2204,7 @@ const member = ({ strapi }) => {
|
|
|
2085
2204
|
const format2 = svc("format");
|
|
2086
2205
|
if (input.phone && seats.some((s) => format2.phoneOf(s) === input.phone))
|
|
2087
2206
|
throw conflict(ERROR_CODES.MEMBER_EXISTS, "Ce numéro est déjà dans le groupe.");
|
|
2088
|
-
const created = await this.seatFor(group2, input);
|
|
2207
|
+
const created = await this.seatFor(group2, input, me.person);
|
|
2089
2208
|
await svc("audit").log({
|
|
2090
2209
|
group: group2,
|
|
2091
2210
|
actor: me.person,
|
|
@@ -2103,17 +2222,23 @@ const member = ({ strapi }) => {
|
|
|
2103
2222
|
if (!seat) throw notFound(ERROR_CODES.MEMBER_NOT_FOUND, "Membre introuvable.");
|
|
2104
2223
|
return seat;
|
|
2105
2224
|
},
|
|
2106
|
-
/**
|
|
2107
|
-
async
|
|
2225
|
+
/** What a seat still has on the books: its balance, and whether a payment awaits an answer. */
|
|
2226
|
+
async standing(group2, seat) {
|
|
2108
2227
|
const books = await svc("ledger").of(group2);
|
|
2109
2228
|
const busy = books.settlements.some(
|
|
2110
2229
|
(s) => s.status === SETTLEMENT_STATUS.PENDING && [s.from, s.to].includes(seat.id)
|
|
2111
2230
|
);
|
|
2112
|
-
|
|
2231
|
+
const balance = books.balanceOf(seat.id);
|
|
2232
|
+
return { balance, settled: balance === 0 && !busy };
|
|
2233
|
+
},
|
|
2234
|
+
/** A seat is only vacated once it is even and nothing awaits confirmation on it. */
|
|
2235
|
+
async assertSettled(group2, seat) {
|
|
2236
|
+
const { balance, settled } = await this.standing(group2, seat);
|
|
2237
|
+
if (!settled)
|
|
2113
2238
|
throw unprocessable(
|
|
2114
2239
|
ERROR_CODES.MEMBER_NOT_SETTLED,
|
|
2115
2240
|
"Les comptes de ce membre ne sont pas à zéro.",
|
|
2116
|
-
{ balance
|
|
2241
|
+
{ balance }
|
|
2117
2242
|
);
|
|
2118
2243
|
},
|
|
2119
2244
|
/** The owner removes someone else. */
|
|
@@ -2147,6 +2272,78 @@ const member = ({ strapi }) => {
|
|
|
2147
2272
|
/** Is this seat held by this person? */
|
|
2148
2273
|
isHeldBy(seat, person2) {
|
|
2149
2274
|
return idOf(seat.person) === person2.id;
|
|
2275
|
+
},
|
|
2276
|
+
/** The invitations a person has not answered, newest first; archived groups are not offered. */
|
|
2277
|
+
async invitations(person2) {
|
|
2278
|
+
const rows = await members().findMany({
|
|
2279
|
+
where: { person: person2.id, joinedAt: { $null: true }, leftAt: { $null: true } },
|
|
2280
|
+
populate: { group: true, invitedBy: true },
|
|
2281
|
+
orderBy: { id: "desc" }
|
|
2282
|
+
});
|
|
2283
|
+
const open = rows.filter((seat) => seat.group && !seat.group.archivedAt);
|
|
2284
|
+
return Promise.all(
|
|
2285
|
+
open.map(async (seat) => ({
|
|
2286
|
+
...seat,
|
|
2287
|
+
memberCount: await members().count({
|
|
2288
|
+
where: { group: seat.group.id, leftAt: { $null: true } }
|
|
2289
|
+
})
|
|
2290
|
+
}))
|
|
2291
|
+
);
|
|
2292
|
+
},
|
|
2293
|
+
/** One open invitation of this person, by the seat's document id. */
|
|
2294
|
+
async invitation(person2, documentId) {
|
|
2295
|
+
const seat = await members().findOne({
|
|
2296
|
+
where: {
|
|
2297
|
+
documentId: String(documentId),
|
|
2298
|
+
person: person2.id,
|
|
2299
|
+
joinedAt: { $null: true },
|
|
2300
|
+
leftAt: { $null: true }
|
|
2301
|
+
},
|
|
2302
|
+
populate: { group: true, person: true }
|
|
2303
|
+
});
|
|
2304
|
+
if (!seat?.group)
|
|
2305
|
+
throw notFound(ERROR_CODES.INVITATION_NOT_FOUND, "Cette invitation n'existe plus.");
|
|
2306
|
+
return seat;
|
|
2307
|
+
},
|
|
2308
|
+
/** The person takes the seat they were offered. */
|
|
2309
|
+
async take(seat, person2) {
|
|
2310
|
+
await members().update({ where: { id: seat.id }, data: { joinedAt: /* @__PURE__ */ new Date() } });
|
|
2311
|
+
await svc("audit").log({
|
|
2312
|
+
group: seat.group,
|
|
2313
|
+
actor: person2,
|
|
2314
|
+
action: EVENT.MEMBER_JOINED,
|
|
2315
|
+
targetId: seat.documentId
|
|
2316
|
+
});
|
|
2317
|
+
},
|
|
2318
|
+
/**
|
|
2319
|
+
* @param {Object} person
|
|
2320
|
+
* @param {string} documentId the invitation's
|
|
2321
|
+
* @returns {Promise<Object>} the group they are now in
|
|
2322
|
+
*/
|
|
2323
|
+
async accept(person2, documentId) {
|
|
2324
|
+
const seat = await this.invitation(person2, documentId);
|
|
2325
|
+
await this.take(seat, person2);
|
|
2326
|
+
return seat.group;
|
|
2327
|
+
},
|
|
2328
|
+
/**
|
|
2329
|
+
* The person says no. A seat with nothing on the books goes; one the group already counted in
|
|
2330
|
+
* its expenses stays as a name nobody holds, so the books still add up.
|
|
2331
|
+
* @param {Object} person
|
|
2332
|
+
* @param {string} documentId the invitation's
|
|
2333
|
+
*/
|
|
2334
|
+
async decline(person2, documentId) {
|
|
2335
|
+
const seat = await this.invitation(person2, documentId);
|
|
2336
|
+
const now = /* @__PURE__ */ new Date();
|
|
2337
|
+
const { settled } = await this.standing(seat.group, seat);
|
|
2338
|
+
const data = settled ? { leftAt: now, declinedAt: now } : { person: null, declinedAt: now };
|
|
2339
|
+
await members().update({ where: { id: seat.id }, data });
|
|
2340
|
+
await svc("audit").log({
|
|
2341
|
+
group: seat.group,
|
|
2342
|
+
actor: person2,
|
|
2343
|
+
action: EVENT.MEMBER_DECLINED,
|
|
2344
|
+
targetId: seat.documentId,
|
|
2345
|
+
data: { kept: !settled }
|
|
2346
|
+
});
|
|
2150
2347
|
}
|
|
2151
2348
|
};
|
|
2152
2349
|
};
|
|
@@ -2393,7 +2590,7 @@ const settlement = ({ strapi }) => {
|
|
|
2393
2590
|
to,
|
|
2394
2591
|
received,
|
|
2395
2592
|
method: String(input.method),
|
|
2396
|
-
instant: received || method.instant || !to
|
|
2593
|
+
instant: received || method.instant || !svc("member").isHeld(to),
|
|
2397
2594
|
reference: text(input?.reference, REF_MAX),
|
|
2398
2595
|
note: text(input?.note, NOTE_MAX),
|
|
2399
2596
|
clientRef: text(input?.clientRef, REF_MAX)
|
|
@@ -2586,8 +2783,10 @@ const orderTotals = ({ items, lines, deliveryFee = 0 }) => {
|
|
|
2586
2783
|
};
|
|
2587
2784
|
const LINE_POPULATE = { item: { select: ["id"] }, member: { select: ["id"] } };
|
|
2588
2785
|
const ORDER_POPULATE = { organizer: { select: ["id"] } };
|
|
2786
|
+
const CURRENT_POPULATE = { group: { select: ["id"] }, organizer: { populate: { person: true } } };
|
|
2589
2787
|
const iso = (date) => date ? new Date(date).toISOString() : null;
|
|
2590
|
-
const
|
|
2788
|
+
const tidy = (raw, max) => String(raw ?? "").trim().replace(/\s+/g, " ").slice(0, max);
|
|
2789
|
+
const label = (raw) => tidy(raw, LABEL_MAX);
|
|
2591
2790
|
const invalid = (message, details) => badRequest(ERROR_CODES.VALIDATION, message, details);
|
|
2592
2791
|
const expenseRefOf = (order2) => `order:${order2.documentId}`;
|
|
2593
2792
|
const order = ({ strapi }) => {
|
|
@@ -2673,7 +2872,8 @@ const order = ({ strapi }) => {
|
|
|
2673
2872
|
lines: taken.map((l) => ({
|
|
2674
2873
|
item: idOf(l.item),
|
|
2675
2874
|
member: idOf(l.member),
|
|
2676
|
-
quantity: l.quantity
|
|
2875
|
+
quantity: l.quantity,
|
|
2876
|
+
note: l.note || null
|
|
2677
2877
|
}))
|
|
2678
2878
|
};
|
|
2679
2879
|
},
|
|
@@ -2690,7 +2890,11 @@ const order = ({ strapi }) => {
|
|
|
2690
2890
|
status: order2.status
|
|
2691
2891
|
});
|
|
2692
2892
|
},
|
|
2693
|
-
/**
|
|
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
|
+
*/
|
|
2694
2898
|
quantities(input, offered) {
|
|
2695
2899
|
const byDoc = new Map(offered.map((item) => [item.documentId, item.id]));
|
|
2696
2900
|
const list = Array.isArray(input?.lines) ? input.lines : null;
|
|
@@ -2704,7 +2908,7 @@ const order = ({ strapi }) => {
|
|
|
2704
2908
|
throw invalid(`Une quantité va de 0 à ${ORDER_MAX_QUANTITY}.`, {
|
|
2705
2909
|
max: ORDER_MAX_QUANTITY
|
|
2706
2910
|
});
|
|
2707
|
-
wanted.set(item, quantity);
|
|
2911
|
+
wanted.set(item, { quantity, note: tidy(raw?.note, ORDER_NOTE_MAX) || null });
|
|
2708
2912
|
}
|
|
2709
2913
|
return wanted;
|
|
2710
2914
|
},
|
|
@@ -2713,7 +2917,7 @@ const order = ({ strapi }) => {
|
|
|
2713
2917
|
* (someone who told them, someone without the app), and still can after the closing time.
|
|
2714
2918
|
* @param {{ group: Object, me: Object }} seat the caller's
|
|
2715
2919
|
* @param {string} documentId the order
|
|
2716
|
-
* @param {{ member?: string, lines: Array<{ item: string, quantity: number }> }} input
|
|
2920
|
+
* @param {{ member?: string, lines: Array<{ item: string, quantity: number, note?: string }> }} input
|
|
2717
2921
|
*/
|
|
2718
2922
|
async setLines({ group: group2, me }, documentId, input) {
|
|
2719
2923
|
const order2 = await this.find(group2, documentId);
|
|
@@ -2730,10 +2934,10 @@ const order = ({ strapi }) => {
|
|
|
2730
2934
|
const wanted = this.quantities(input, offered);
|
|
2731
2935
|
await strapi.db.transaction(async () => {
|
|
2732
2936
|
await lines().deleteMany({ where: { order: order2.id, member: target.id } });
|
|
2733
|
-
for (const [item, quantity] of wanted)
|
|
2937
|
+
for (const [item, { quantity, note }] of wanted)
|
|
2734
2938
|
if (quantity > 0)
|
|
2735
2939
|
await lines().create({
|
|
2736
|
-
data: { order: order2.id, item, member: target.id, quantity }
|
|
2940
|
+
data: { order: order2.id, item, member: target.id, quantity, note }
|
|
2737
2941
|
});
|
|
2738
2942
|
});
|
|
2739
2943
|
return order2;
|
|
@@ -2848,7 +3052,7 @@ const order = ({ strapi }) => {
|
|
|
2848
3052
|
yours
|
|
2849
3053
|
};
|
|
2850
3054
|
if (!detailed) return summary;
|
|
2851
|
-
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 }));
|
|
2852
3056
|
const quantityOf = (item, member2) => taken.filter((l) => l.item === item && (member2 == null || l.member === member2)).reduce((sum, l) => sum + l.quantity, 0);
|
|
2853
3057
|
return {
|
|
2854
3058
|
...summary,
|
|
@@ -2866,6 +3070,60 @@ const order = ({ strapi }) => {
|
|
|
2866
3070
|
}))
|
|
2867
3071
|
};
|
|
2868
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
|
+
},
|
|
2869
3127
|
/** The group's orders as a member reads them: the open ones first, then the latest. */
|
|
2870
3128
|
async list(seat, viewer) {
|
|
2871
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>",
|