@sonatel-os/strapi-plugin-split 0.3.0 → 0.4.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 +5 -2
- package/dist/server/index.js +204 -19
- package/dist/server/index.mjs +204 -19
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -10,9 +10,10 @@ It assumes nothing about its host. Who the people are (a header your gateway set
|
|
|
10
10
|
|
|
11
11
|
| Area | In the admin | For the app (`/api/split`) |
|
|
12
12
|
| --------------- | ---------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------ |
|
|
13
|
-
| **People** | Search by name or phone (masked), reveal a full number (permission, logged), deactivate. | `GET /me`
|
|
13
|
+
| **People** | Search by name or phone (masked), reveal a full number (permission, logged), deactivate. | `GET /me`, `PUT /me` (`{ autoAcceptInvites }`) |
|
|
14
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
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` |
|
|
16
17
|
| **Members** | Seats with their state: uses the app, added by name, left. | `POST /groups/:group/members`, `DELETE /groups/:group/members/:member` |
|
|
17
18
|
| **Expenses** | The most recent fifty of a group. | `GET/POST /groups/:group/expenses`, `PUT/DELETE …/expenses/:expense` |
|
|
18
19
|
| **Settlements** | Look a payment up by the reference the customer has. | `GET/POST /groups/:group/settlements`, `POST …/settlements/:settlement/confirm`, `…/reject`, `…/cancel` |
|
|
@@ -27,7 +28,9 @@ 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
|
+
- **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.
|
|
31
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.
|
|
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
|
|
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",
|
|
@@ -106,6 +113,7 @@ const ERROR_CODES = Object.freeze({
|
|
|
106
113
|
NOT_A_MEMBER: "NOT_A_MEMBER",
|
|
107
114
|
NOT_ALLOWED: "NOT_ALLOWED",
|
|
108
115
|
INVITE_INVALID: "INVITE_INVALID",
|
|
116
|
+
INVITATION_NOT_FOUND: "INVITATION_NOT_FOUND",
|
|
109
117
|
MEMBER_NOT_FOUND: "MEMBER_NOT_FOUND",
|
|
110
118
|
MEMBER_EXISTS: "MEMBER_EXISTS",
|
|
111
119
|
MEMBER_LIMIT: "MEMBER_LIMIT",
|
|
@@ -274,6 +282,10 @@ const attributes$9 = {
|
|
|
274
282
|
lastActiveAt: {
|
|
275
283
|
type: "datetime"
|
|
276
284
|
},
|
|
285
|
+
autoAcceptInvites: {
|
|
286
|
+
type: "boolean",
|
|
287
|
+
"default": false
|
|
288
|
+
},
|
|
277
289
|
memberships: {
|
|
278
290
|
type: "relation",
|
|
279
291
|
relation: "oneToMany",
|
|
@@ -354,7 +366,7 @@ const info$7 = {
|
|
|
354
366
|
singularName: "member",
|
|
355
367
|
pluralName: "members",
|
|
356
368
|
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."
|
|
369
|
+
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
370
|
};
|
|
359
371
|
const options$7 = {
|
|
360
372
|
draftAndPublish: false
|
|
@@ -404,6 +416,14 @@ const attributes$7 = {
|
|
|
404
416
|
},
|
|
405
417
|
leftAt: {
|
|
406
418
|
type: "datetime"
|
|
419
|
+
},
|
|
420
|
+
invitedBy: {
|
|
421
|
+
type: "relation",
|
|
422
|
+
relation: "manyToOne",
|
|
423
|
+
target: "plugin::split.person"
|
|
424
|
+
},
|
|
425
|
+
declinedAt: {
|
|
426
|
+
type: "datetime"
|
|
407
427
|
}
|
|
408
428
|
};
|
|
409
429
|
const schema$7 = {
|
|
@@ -957,12 +977,33 @@ const app = ({ strapi }) => {
|
|
|
957
977
|
me: handle(strapi, async (ctx) => {
|
|
958
978
|
ok(ctx, svc("format").person(viewer(ctx), Boolean(ctx.state.splitPersonIsNew)));
|
|
959
979
|
}),
|
|
980
|
+
updateMe: handle(strapi, async (ctx) => {
|
|
981
|
+
const person2 = await svc("person").setPreferences(viewer(ctx), body(ctx));
|
|
982
|
+
ok(ctx, svc("format").person(person2));
|
|
983
|
+
}),
|
|
960
984
|
overview: handle(strapi, async (ctx) => ok(ctx, await svc("view").overview(viewer(ctx)))),
|
|
961
985
|
activity: handle(strapi, async (ctx) => {
|
|
962
986
|
const page = parsePagination(ctx.query);
|
|
963
987
|
const { items, total } = await svc("view").activity(viewer(ctx), page);
|
|
964
988
|
ctx.body = paged(items, total, page);
|
|
965
989
|
}),
|
|
990
|
+
// ── Invitations ──────────────────────────────────────────────────────
|
|
991
|
+
invitations: handle(strapi, async (ctx) => {
|
|
992
|
+
const seats = await svc("member").invitations(viewer(ctx));
|
|
993
|
+
ok(
|
|
994
|
+
ctx,
|
|
995
|
+
seats.map((s) => svc("format").invitation(s))
|
|
996
|
+
);
|
|
997
|
+
}),
|
|
998
|
+
acceptInvitation: handle(strapi, async (ctx) => {
|
|
999
|
+
const group2 = await svc("member").accept(viewer(ctx), ctx.params.invitation);
|
|
1000
|
+
const mine = await svc("group").seat(viewer(ctx), group2.documentId);
|
|
1001
|
+
ok(ctx, await svc("view").group(mine, viewer(ctx)));
|
|
1002
|
+
}),
|
|
1003
|
+
declineInvitation: handle(strapi, async (ctx) => {
|
|
1004
|
+
await svc("member").decline(viewer(ctx), ctx.params.invitation);
|
|
1005
|
+
noContent(ctx);
|
|
1006
|
+
}),
|
|
966
1007
|
// ── Groups ───────────────────────────────────────────────────────────
|
|
967
1008
|
createGroup: handle(strapi, async (ctx) => {
|
|
968
1009
|
const group2 = await svc("group").create(viewer(ctx), body(ctx));
|
|
@@ -1194,11 +1235,13 @@ const view = ({ strapi }) => {
|
|
|
1194
1235
|
const debts = perGroup.flatMap((g) => g.debts).sort((a, b) => b.amount - a.amount);
|
|
1195
1236
|
const credits = perGroup.flatMap((g) => g.credits).sort((a, b) => b.amount - a.amount);
|
|
1196
1237
|
const sum = (list) => list.reduce((total, item) => total + item.amount, 0);
|
|
1238
|
+
const invitations = await svc("member").invitations(viewer);
|
|
1197
1239
|
return {
|
|
1198
1240
|
owe: sum(debts),
|
|
1199
1241
|
owed: sum(credits),
|
|
1200
1242
|
debts,
|
|
1201
1243
|
credits,
|
|
1244
|
+
invitations: invitations.map((seat) => format2.invitation(seat)),
|
|
1202
1245
|
groups: perGroup.map((g) => g.summary).sort((a, b) => new Date(b.lastActivityAt ?? 0) - new Date(a.lastActivityAt ?? 0))
|
|
1203
1246
|
};
|
|
1204
1247
|
},
|
|
@@ -1405,8 +1448,14 @@ const route$1 = (method, path, handler) => ({
|
|
|
1405
1448
|
const contentApi = [
|
|
1406
1449
|
// person
|
|
1407
1450
|
route$1("GET", "/me", "me"),
|
|
1451
|
+
// PUT, not PATCH: some app runtimes (mini-programs) cannot send PATCH.
|
|
1452
|
+
route$1("PUT", "/me", "updateMe"),
|
|
1408
1453
|
route$1("GET", "/overview", "overview"),
|
|
1409
1454
|
route$1("GET", "/activity", "activity"),
|
|
1455
|
+
// invitations: being added to a group is an offer, answered by the person
|
|
1456
|
+
route$1("GET", "/invitations", "invitations"),
|
|
1457
|
+
route$1("POST", "/invitations/:invitation/accept", "acceptInvitation"),
|
|
1458
|
+
route$1("POST", "/invitations/:invitation/decline", "declineInvitation"),
|
|
1410
1459
|
// groups
|
|
1411
1460
|
route$1("POST", "/groups", "createGroup"),
|
|
1412
1461
|
route$1("POST", "/groups/join", "joinGroup"),
|
|
@@ -1541,7 +1590,13 @@ const audit = ({ strapi }) => ({
|
|
|
1541
1590
|
const iso$1 = (date) => date ? new Date(date).toISOString() : null;
|
|
1542
1591
|
const format = () => ({
|
|
1543
1592
|
person(p, isNew = false) {
|
|
1544
|
-
return {
|
|
1593
|
+
return {
|
|
1594
|
+
id: p.documentId,
|
|
1595
|
+
displayName: p.displayName ?? null,
|
|
1596
|
+
phone: p.phone ?? null,
|
|
1597
|
+
autoAcceptInvites: Boolean(p.autoAcceptInvites),
|
|
1598
|
+
isNew
|
|
1599
|
+
};
|
|
1545
1600
|
},
|
|
1546
1601
|
/** The name a seat goes by: the person's own once they have one, the name it was added under otherwise. */
|
|
1547
1602
|
nameOf(member2) {
|
|
@@ -1561,10 +1616,29 @@ const format = () => ({
|
|
|
1561
1616
|
displayName: this.nameOf(m),
|
|
1562
1617
|
isYou: Boolean(viewer) && idOf(m.person) === viewer.id,
|
|
1563
1618
|
isRegistered: Boolean(m.person),
|
|
1619
|
+
invite: this.inviteOf(m),
|
|
1564
1620
|
role: m.role,
|
|
1565
1621
|
hasLeft: Boolean(m.leftAt)
|
|
1566
1622
|
};
|
|
1567
1623
|
},
|
|
1624
|
+
/** `pending` while an invited person has not answered, `declined` once they said no, else null. */
|
|
1625
|
+
inviteOf(m) {
|
|
1626
|
+
if (m.declinedAt) return INVITE.DECLINED;
|
|
1627
|
+
return m.person && !m.joinedAt ? INVITE.PENDING : null;
|
|
1628
|
+
},
|
|
1629
|
+
/**
|
|
1630
|
+
* An invitation as the invited person sees it: enough to decide, nothing of the books.
|
|
1631
|
+
* @param {Object} seat member row, `group` and `invitedBy` populated, with a `memberCount`
|
|
1632
|
+
*/
|
|
1633
|
+
invitation(seat) {
|
|
1634
|
+
return {
|
|
1635
|
+
id: seat.documentId,
|
|
1636
|
+
group: { id: seat.group.documentId, name: seat.group.name },
|
|
1637
|
+
invitedBy: seat.invitedBy ? { displayName: seat.invitedBy.displayName ?? null } : null,
|
|
1638
|
+
memberCount: seat.memberCount,
|
|
1639
|
+
invitedAt: iso$1(seat.createdAt)
|
|
1640
|
+
};
|
|
1641
|
+
},
|
|
1568
1642
|
/** A seat as an administrator sees it: the phone masked. */
|
|
1569
1643
|
adminMember(m) {
|
|
1570
1644
|
return { ...this.member(m), phone: maskPhone(this.phoneOf(m)), joinedAt: iso$1(m.joinedAt) };
|
|
@@ -1678,7 +1752,8 @@ const person = ({ strapi }) => ({
|
|
|
1678
1752
|
}
|
|
1679
1753
|
},
|
|
1680
1754
|
/**
|
|
1681
|
-
* Seats added by phone before this person existed become theirs
|
|
1755
|
+
* Seats added by phone before this person existed become invitations to them: theirs to accept
|
|
1756
|
+
* or decline, like any other.
|
|
1682
1757
|
* @param {Object} person
|
|
1683
1758
|
* @returns {Promise<number>} seats claimed
|
|
1684
1759
|
*/
|
|
@@ -1689,10 +1764,24 @@ const person = ({ strapi }) => ({
|
|
|
1689
1764
|
select: ["id"]
|
|
1690
1765
|
});
|
|
1691
1766
|
for (const seat of seats) {
|
|
1692
|
-
await strapi.db.query(UID.MEMBER).update({ where: { id: seat.id }, data: { person: person2.id
|
|
1767
|
+
await strapi.db.query(UID.MEMBER).update({ where: { id: seat.id }, data: { person: person2.id } });
|
|
1693
1768
|
}
|
|
1694
1769
|
return seats.length;
|
|
1695
1770
|
},
|
|
1771
|
+
/**
|
|
1772
|
+
* What a person chose for themselves.
|
|
1773
|
+
* @param {Object} person
|
|
1774
|
+
* @param {{ autoAcceptInvites?: boolean }} input
|
|
1775
|
+
* @returns {Promise<Object>} the person, updated
|
|
1776
|
+
*/
|
|
1777
|
+
async setPreferences(person2, input) {
|
|
1778
|
+
if (typeof input?.autoAcceptInvites !== "boolean")
|
|
1779
|
+
throw badRequest(ERROR_CODES.VALIDATION, "Préférence invalide.");
|
|
1780
|
+
return strapi.db.query(UID.PERSON).update({
|
|
1781
|
+
where: { id: person2.id },
|
|
1782
|
+
data: { autoAcceptInvites: input.autoAcceptInvites }
|
|
1783
|
+
});
|
|
1784
|
+
},
|
|
1696
1785
|
/** The registered person with this phone, if any. */
|
|
1697
1786
|
async byPhone(phone) {
|
|
1698
1787
|
return phone ? strapi.db.query(UID.PERSON).findOne({ where: { phone } }) : null;
|
|
@@ -1847,6 +1936,7 @@ const ledger = ({ strapi }) => ({
|
|
|
1847
1936
|
return books.settlements.filter((s) => s.status === SETTLEMENT_STATUS.PENDING && s.from === from && s.to === to).reduce((sum, s) => sum + s.amount, 0);
|
|
1848
1937
|
}
|
|
1849
1938
|
});
|
|
1939
|
+
const HELD = Object.freeze({ leftAt: { $null: true }, joinedAt: { $notNull: true } });
|
|
1850
1940
|
const cleanName = (raw) => String(raw ?? "").trim().replace(/\s+/g, " ").slice(0, NAME_MAX);
|
|
1851
1941
|
const group = ({ strapi }) => {
|
|
1852
1942
|
const svc = (name) => strapi.plugin(PLUGIN_ID).service(name);
|
|
@@ -1873,7 +1963,7 @@ const group = ({ strapi }) => {
|
|
|
1873
1963
|
async seat(person2, documentId, { writable = false } = {}) {
|
|
1874
1964
|
const group2 = await groups().findOne({ where: { documentId: String(documentId) } });
|
|
1875
1965
|
const me = group2 && await members().findOne({
|
|
1876
|
-
where: { group: group2.id, person: person2.id,
|
|
1966
|
+
where: { group: group2.id, person: person2.id, ...HELD },
|
|
1877
1967
|
populate: { person: true }
|
|
1878
1968
|
});
|
|
1879
1969
|
if (!me) throw notFound(ERROR_CODES.GROUP_NOT_FOUND, "Groupe introuvable.");
|
|
@@ -1889,7 +1979,7 @@ const group = ({ strapi }) => {
|
|
|
1889
1979
|
/** The seats a person holds, groups populated; archived groups only on request. */
|
|
1890
1980
|
async seatsOf(person2, { archived = false } = {}) {
|
|
1891
1981
|
const rows = await members().findMany({
|
|
1892
|
-
where: { person: person2.id,
|
|
1982
|
+
where: { person: person2.id, ...HELD },
|
|
1893
1983
|
populate: { group: true, person: true }
|
|
1894
1984
|
});
|
|
1895
1985
|
return rows.filter((m) => m.group && Boolean(m.group.archivedAt) === archived);
|
|
@@ -1906,7 +1996,7 @@ const group = ({ strapi }) => {
|
|
|
1906
1996
|
const name = cleanName(input.name);
|
|
1907
1997
|
if (!name) throw badRequest(ERROR_CODES.VALIDATION, "Donne un nom au groupe.");
|
|
1908
1998
|
const limits = svc("settings").limits();
|
|
1909
|
-
const held = await members().count({ where: { person: person2.id,
|
|
1999
|
+
const held = await members().count({ where: { person: person2.id, ...HELD } });
|
|
1910
2000
|
if (held >= limits.maxGroupsPerPerson)
|
|
1911
2001
|
throw unprocessable(
|
|
1912
2002
|
ERROR_CODES.GROUP_LIMIT,
|
|
@@ -1934,7 +2024,7 @@ const group = ({ strapi }) => {
|
|
|
1934
2024
|
joinedAt: now
|
|
1935
2025
|
}
|
|
1936
2026
|
});
|
|
1937
|
-
for (const other of others) await svc("member").seatFor(g, other);
|
|
2027
|
+
for (const other of others) await svc("member").seatFor(g, other, person2);
|
|
1938
2028
|
return g;
|
|
1939
2029
|
});
|
|
1940
2030
|
await svc("audit").log({
|
|
@@ -1969,8 +2059,8 @@ const group = ({ strapi }) => {
|
|
|
1969
2059
|
return updated;
|
|
1970
2060
|
},
|
|
1971
2061
|
/**
|
|
1972
|
-
* Take a seat with an invite code.
|
|
1973
|
-
*
|
|
2062
|
+
* Take a seat with an invite code. Entering the code of a group one was invited to accepts the
|
|
2063
|
+
* invitation; joining twice is not an error.
|
|
1974
2064
|
*/
|
|
1975
2065
|
async join(person2, rawCode) {
|
|
1976
2066
|
const inviteCode = String(rawCode ?? "").trim().toUpperCase();
|
|
@@ -1980,6 +2070,7 @@ const group = ({ strapi }) => {
|
|
|
1980
2070
|
const existing = await members().findOne({
|
|
1981
2071
|
where: { group: group2.id, person: person2.id, leftAt: { $null: true } }
|
|
1982
2072
|
});
|
|
2073
|
+
if (existing && !existing.joinedAt) await svc("member").take({ ...existing, group: group2 }, person2);
|
|
1983
2074
|
if (existing) return group2;
|
|
1984
2075
|
const limits = svc("settings").limits();
|
|
1985
2076
|
const count = await members().count({ where: { group: group2.id, leftAt: { $null: true } } });
|
|
@@ -2052,8 +2143,14 @@ const member = ({ strapi }) => {
|
|
|
2052
2143
|
throw conflict(ERROR_CODES.MEMBER_EXISTS, "Un numéro apparaît deux fois.");
|
|
2053
2144
|
return parsed;
|
|
2054
2145
|
},
|
|
2055
|
-
/**
|
|
2056
|
-
|
|
2146
|
+
/**
|
|
2147
|
+
* Create the seat. When that phone already uses the app it is an invitation to them, taken at
|
|
2148
|
+
* once only for someone who accepts every invitation.
|
|
2149
|
+
* @param {Object} group
|
|
2150
|
+
* @param {{ displayName: string, phone: string|null }} input
|
|
2151
|
+
* @param {Object} inviter the person adding them
|
|
2152
|
+
*/
|
|
2153
|
+
async seatFor(group2, { displayName, phone }, inviter) {
|
|
2057
2154
|
const person2 = await svc("person").byPhone(phone);
|
|
2058
2155
|
return members().create({
|
|
2059
2156
|
data: {
|
|
@@ -2062,10 +2159,20 @@ const member = ({ strapi }) => {
|
|
|
2062
2159
|
displayName,
|
|
2063
2160
|
phone,
|
|
2064
2161
|
role: ROLE.MEMBER,
|
|
2065
|
-
|
|
2162
|
+
invitedBy: inviter?.id ?? null,
|
|
2163
|
+
joinedAt: person2?.autoAcceptInvites ? /* @__PURE__ */ new Date() : null
|
|
2066
2164
|
}
|
|
2067
2165
|
});
|
|
2068
2166
|
},
|
|
2167
|
+
/** Is someone sitting here: a person who is in the group, not a name or an open invitation? */
|
|
2168
|
+
isHeld(seat) {
|
|
2169
|
+
return Boolean(seat.person) && Boolean(seat.joinedAt);
|
|
2170
|
+
},
|
|
2171
|
+
/** Where an invitation stands, or null for a seat that is held or was never anyone's. */
|
|
2172
|
+
inviteOf(seat) {
|
|
2173
|
+
if (seat.declinedAt) return INVITE.DECLINED;
|
|
2174
|
+
return seat.person && !seat.joinedAt ? INVITE.PENDING : null;
|
|
2175
|
+
},
|
|
2069
2176
|
/**
|
|
2070
2177
|
* @param {{ group: Object, me: Object }} seat the caller's
|
|
2071
2178
|
* @param {{ name: string, phone?: string }} raw
|
|
@@ -2086,7 +2193,7 @@ const member = ({ strapi }) => {
|
|
|
2086
2193
|
const format2 = svc("format");
|
|
2087
2194
|
if (input.phone && seats.some((s) => format2.phoneOf(s) === input.phone))
|
|
2088
2195
|
throw conflict(ERROR_CODES.MEMBER_EXISTS, "Ce numéro est déjà dans le groupe.");
|
|
2089
|
-
const created = await this.seatFor(group2, input);
|
|
2196
|
+
const created = await this.seatFor(group2, input, me.person);
|
|
2090
2197
|
await svc("audit").log({
|
|
2091
2198
|
group: group2,
|
|
2092
2199
|
actor: me.person,
|
|
@@ -2104,17 +2211,23 @@ const member = ({ strapi }) => {
|
|
|
2104
2211
|
if (!seat) throw notFound(ERROR_CODES.MEMBER_NOT_FOUND, "Membre introuvable.");
|
|
2105
2212
|
return seat;
|
|
2106
2213
|
},
|
|
2107
|
-
/**
|
|
2108
|
-
async
|
|
2214
|
+
/** What a seat still has on the books: its balance, and whether a payment awaits an answer. */
|
|
2215
|
+
async standing(group2, seat) {
|
|
2109
2216
|
const books = await svc("ledger").of(group2);
|
|
2110
2217
|
const busy = books.settlements.some(
|
|
2111
2218
|
(s) => s.status === SETTLEMENT_STATUS.PENDING && [s.from, s.to].includes(seat.id)
|
|
2112
2219
|
);
|
|
2113
|
-
|
|
2220
|
+
const balance = books.balanceOf(seat.id);
|
|
2221
|
+
return { balance, settled: balance === 0 && !busy };
|
|
2222
|
+
},
|
|
2223
|
+
/** A seat is only vacated once it is even and nothing awaits confirmation on it. */
|
|
2224
|
+
async assertSettled(group2, seat) {
|
|
2225
|
+
const { balance, settled } = await this.standing(group2, seat);
|
|
2226
|
+
if (!settled)
|
|
2114
2227
|
throw unprocessable(
|
|
2115
2228
|
ERROR_CODES.MEMBER_NOT_SETTLED,
|
|
2116
2229
|
"Les comptes de ce membre ne sont pas à zéro.",
|
|
2117
|
-
{ balance
|
|
2230
|
+
{ balance }
|
|
2118
2231
|
);
|
|
2119
2232
|
},
|
|
2120
2233
|
/** The owner removes someone else. */
|
|
@@ -2148,6 +2261,78 @@ const member = ({ strapi }) => {
|
|
|
2148
2261
|
/** Is this seat held by this person? */
|
|
2149
2262
|
isHeldBy(seat, person2) {
|
|
2150
2263
|
return idOf(seat.person) === person2.id;
|
|
2264
|
+
},
|
|
2265
|
+
/** The invitations a person has not answered, newest first; archived groups are not offered. */
|
|
2266
|
+
async invitations(person2) {
|
|
2267
|
+
const rows = await members().findMany({
|
|
2268
|
+
where: { person: person2.id, joinedAt: { $null: true }, leftAt: { $null: true } },
|
|
2269
|
+
populate: { group: true, invitedBy: true },
|
|
2270
|
+
orderBy: { id: "desc" }
|
|
2271
|
+
});
|
|
2272
|
+
const open = rows.filter((seat) => seat.group && !seat.group.archivedAt);
|
|
2273
|
+
return Promise.all(
|
|
2274
|
+
open.map(async (seat) => ({
|
|
2275
|
+
...seat,
|
|
2276
|
+
memberCount: await members().count({
|
|
2277
|
+
where: { group: seat.group.id, leftAt: { $null: true } }
|
|
2278
|
+
})
|
|
2279
|
+
}))
|
|
2280
|
+
);
|
|
2281
|
+
},
|
|
2282
|
+
/** One open invitation of this person, by the seat's document id. */
|
|
2283
|
+
async invitation(person2, documentId) {
|
|
2284
|
+
const seat = await members().findOne({
|
|
2285
|
+
where: {
|
|
2286
|
+
documentId: String(documentId),
|
|
2287
|
+
person: person2.id,
|
|
2288
|
+
joinedAt: { $null: true },
|
|
2289
|
+
leftAt: { $null: true }
|
|
2290
|
+
},
|
|
2291
|
+
populate: { group: true, person: true }
|
|
2292
|
+
});
|
|
2293
|
+
if (!seat?.group)
|
|
2294
|
+
throw notFound(ERROR_CODES.INVITATION_NOT_FOUND, "Cette invitation n'existe plus.");
|
|
2295
|
+
return seat;
|
|
2296
|
+
},
|
|
2297
|
+
/** The person takes the seat they were offered. */
|
|
2298
|
+
async take(seat, person2) {
|
|
2299
|
+
await members().update({ where: { id: seat.id }, data: { joinedAt: /* @__PURE__ */ new Date() } });
|
|
2300
|
+
await svc("audit").log({
|
|
2301
|
+
group: seat.group,
|
|
2302
|
+
actor: person2,
|
|
2303
|
+
action: EVENT.MEMBER_JOINED,
|
|
2304
|
+
targetId: seat.documentId
|
|
2305
|
+
});
|
|
2306
|
+
},
|
|
2307
|
+
/**
|
|
2308
|
+
* @param {Object} person
|
|
2309
|
+
* @param {string} documentId the invitation's
|
|
2310
|
+
* @returns {Promise<Object>} the group they are now in
|
|
2311
|
+
*/
|
|
2312
|
+
async accept(person2, documentId) {
|
|
2313
|
+
const seat = await this.invitation(person2, documentId);
|
|
2314
|
+
await this.take(seat, person2);
|
|
2315
|
+
return seat.group;
|
|
2316
|
+
},
|
|
2317
|
+
/**
|
|
2318
|
+
* The person says no. A seat with nothing on the books goes; one the group already counted in
|
|
2319
|
+
* its expenses stays as a name nobody holds, so the books still add up.
|
|
2320
|
+
* @param {Object} person
|
|
2321
|
+
* @param {string} documentId the invitation's
|
|
2322
|
+
*/
|
|
2323
|
+
async decline(person2, documentId) {
|
|
2324
|
+
const seat = await this.invitation(person2, documentId);
|
|
2325
|
+
const now = /* @__PURE__ */ new Date();
|
|
2326
|
+
const { settled } = await this.standing(seat.group, seat);
|
|
2327
|
+
const data = settled ? { leftAt: now, declinedAt: now } : { person: null, declinedAt: now };
|
|
2328
|
+
await members().update({ where: { id: seat.id }, data });
|
|
2329
|
+
await svc("audit").log({
|
|
2330
|
+
group: seat.group,
|
|
2331
|
+
actor: person2,
|
|
2332
|
+
action: EVENT.MEMBER_DECLINED,
|
|
2333
|
+
targetId: seat.documentId,
|
|
2334
|
+
data: { kept: !settled }
|
|
2335
|
+
});
|
|
2151
2336
|
}
|
|
2152
2337
|
};
|
|
2153
2338
|
};
|
|
@@ -2394,7 +2579,7 @@ const settlement = ({ strapi }) => {
|
|
|
2394
2579
|
to,
|
|
2395
2580
|
received,
|
|
2396
2581
|
method: String(input.method),
|
|
2397
|
-
instant: received || method.instant || !to
|
|
2582
|
+
instant: received || method.instant || !svc("member").isHeld(to),
|
|
2398
2583
|
reference: text(input?.reference, REF_MAX),
|
|
2399
2584
|
note: text(input?.note, NOTE_MAX),
|
|
2400
2585
|
clientRef: text(input?.clientRef, REF_MAX)
|
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",
|
|
@@ -105,6 +112,7 @@ const ERROR_CODES = Object.freeze({
|
|
|
105
112
|
NOT_A_MEMBER: "NOT_A_MEMBER",
|
|
106
113
|
NOT_ALLOWED: "NOT_ALLOWED",
|
|
107
114
|
INVITE_INVALID: "INVITE_INVALID",
|
|
115
|
+
INVITATION_NOT_FOUND: "INVITATION_NOT_FOUND",
|
|
108
116
|
MEMBER_NOT_FOUND: "MEMBER_NOT_FOUND",
|
|
109
117
|
MEMBER_EXISTS: "MEMBER_EXISTS",
|
|
110
118
|
MEMBER_LIMIT: "MEMBER_LIMIT",
|
|
@@ -273,6 +281,10 @@ const attributes$9 = {
|
|
|
273
281
|
lastActiveAt: {
|
|
274
282
|
type: "datetime"
|
|
275
283
|
},
|
|
284
|
+
autoAcceptInvites: {
|
|
285
|
+
type: "boolean",
|
|
286
|
+
"default": false
|
|
287
|
+
},
|
|
276
288
|
memberships: {
|
|
277
289
|
type: "relation",
|
|
278
290
|
relation: "oneToMany",
|
|
@@ -353,7 +365,7 @@ const info$7 = {
|
|
|
353
365
|
singularName: "member",
|
|
354
366
|
pluralName: "members",
|
|
355
367
|
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."
|
|
368
|
+
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
369
|
};
|
|
358
370
|
const options$7 = {
|
|
359
371
|
draftAndPublish: false
|
|
@@ -403,6 +415,14 @@ const attributes$7 = {
|
|
|
403
415
|
},
|
|
404
416
|
leftAt: {
|
|
405
417
|
type: "datetime"
|
|
418
|
+
},
|
|
419
|
+
invitedBy: {
|
|
420
|
+
type: "relation",
|
|
421
|
+
relation: "manyToOne",
|
|
422
|
+
target: "plugin::split.person"
|
|
423
|
+
},
|
|
424
|
+
declinedAt: {
|
|
425
|
+
type: "datetime"
|
|
406
426
|
}
|
|
407
427
|
};
|
|
408
428
|
const schema$7 = {
|
|
@@ -956,12 +976,33 @@ const app = ({ strapi }) => {
|
|
|
956
976
|
me: handle(strapi, async (ctx) => {
|
|
957
977
|
ok(ctx, svc("format").person(viewer(ctx), Boolean(ctx.state.splitPersonIsNew)));
|
|
958
978
|
}),
|
|
979
|
+
updateMe: handle(strapi, async (ctx) => {
|
|
980
|
+
const person2 = await svc("person").setPreferences(viewer(ctx), body(ctx));
|
|
981
|
+
ok(ctx, svc("format").person(person2));
|
|
982
|
+
}),
|
|
959
983
|
overview: handle(strapi, async (ctx) => ok(ctx, await svc("view").overview(viewer(ctx)))),
|
|
960
984
|
activity: handle(strapi, async (ctx) => {
|
|
961
985
|
const page = parsePagination(ctx.query);
|
|
962
986
|
const { items, total } = await svc("view").activity(viewer(ctx), page);
|
|
963
987
|
ctx.body = paged(items, total, page);
|
|
964
988
|
}),
|
|
989
|
+
// ── Invitations ──────────────────────────────────────────────────────
|
|
990
|
+
invitations: handle(strapi, async (ctx) => {
|
|
991
|
+
const seats = await svc("member").invitations(viewer(ctx));
|
|
992
|
+
ok(
|
|
993
|
+
ctx,
|
|
994
|
+
seats.map((s) => svc("format").invitation(s))
|
|
995
|
+
);
|
|
996
|
+
}),
|
|
997
|
+
acceptInvitation: handle(strapi, async (ctx) => {
|
|
998
|
+
const group2 = await svc("member").accept(viewer(ctx), ctx.params.invitation);
|
|
999
|
+
const mine = await svc("group").seat(viewer(ctx), group2.documentId);
|
|
1000
|
+
ok(ctx, await svc("view").group(mine, viewer(ctx)));
|
|
1001
|
+
}),
|
|
1002
|
+
declineInvitation: handle(strapi, async (ctx) => {
|
|
1003
|
+
await svc("member").decline(viewer(ctx), ctx.params.invitation);
|
|
1004
|
+
noContent(ctx);
|
|
1005
|
+
}),
|
|
965
1006
|
// ── Groups ───────────────────────────────────────────────────────────
|
|
966
1007
|
createGroup: handle(strapi, async (ctx) => {
|
|
967
1008
|
const group2 = await svc("group").create(viewer(ctx), body(ctx));
|
|
@@ -1193,11 +1234,13 @@ const view = ({ strapi }) => {
|
|
|
1193
1234
|
const debts = perGroup.flatMap((g) => g.debts).sort((a, b) => b.amount - a.amount);
|
|
1194
1235
|
const credits = perGroup.flatMap((g) => g.credits).sort((a, b) => b.amount - a.amount);
|
|
1195
1236
|
const sum = (list) => list.reduce((total, item) => total + item.amount, 0);
|
|
1237
|
+
const invitations = await svc("member").invitations(viewer);
|
|
1196
1238
|
return {
|
|
1197
1239
|
owe: sum(debts),
|
|
1198
1240
|
owed: sum(credits),
|
|
1199
1241
|
debts,
|
|
1200
1242
|
credits,
|
|
1243
|
+
invitations: invitations.map((seat) => format2.invitation(seat)),
|
|
1201
1244
|
groups: perGroup.map((g) => g.summary).sort((a, b) => new Date(b.lastActivityAt ?? 0) - new Date(a.lastActivityAt ?? 0))
|
|
1202
1245
|
};
|
|
1203
1246
|
},
|
|
@@ -1404,8 +1447,14 @@ const route$1 = (method, path, handler) => ({
|
|
|
1404
1447
|
const contentApi = [
|
|
1405
1448
|
// person
|
|
1406
1449
|
route$1("GET", "/me", "me"),
|
|
1450
|
+
// PUT, not PATCH: some app runtimes (mini-programs) cannot send PATCH.
|
|
1451
|
+
route$1("PUT", "/me", "updateMe"),
|
|
1407
1452
|
route$1("GET", "/overview", "overview"),
|
|
1408
1453
|
route$1("GET", "/activity", "activity"),
|
|
1454
|
+
// invitations: being added to a group is an offer, answered by the person
|
|
1455
|
+
route$1("GET", "/invitations", "invitations"),
|
|
1456
|
+
route$1("POST", "/invitations/:invitation/accept", "acceptInvitation"),
|
|
1457
|
+
route$1("POST", "/invitations/:invitation/decline", "declineInvitation"),
|
|
1409
1458
|
// groups
|
|
1410
1459
|
route$1("POST", "/groups", "createGroup"),
|
|
1411
1460
|
route$1("POST", "/groups/join", "joinGroup"),
|
|
@@ -1540,7 +1589,13 @@ const audit = ({ strapi }) => ({
|
|
|
1540
1589
|
const iso$1 = (date) => date ? new Date(date).toISOString() : null;
|
|
1541
1590
|
const format = () => ({
|
|
1542
1591
|
person(p, isNew = false) {
|
|
1543
|
-
return {
|
|
1592
|
+
return {
|
|
1593
|
+
id: p.documentId,
|
|
1594
|
+
displayName: p.displayName ?? null,
|
|
1595
|
+
phone: p.phone ?? null,
|
|
1596
|
+
autoAcceptInvites: Boolean(p.autoAcceptInvites),
|
|
1597
|
+
isNew
|
|
1598
|
+
};
|
|
1544
1599
|
},
|
|
1545
1600
|
/** The name a seat goes by: the person's own once they have one, the name it was added under otherwise. */
|
|
1546
1601
|
nameOf(member2) {
|
|
@@ -1560,10 +1615,29 @@ const format = () => ({
|
|
|
1560
1615
|
displayName: this.nameOf(m),
|
|
1561
1616
|
isYou: Boolean(viewer) && idOf(m.person) === viewer.id,
|
|
1562
1617
|
isRegistered: Boolean(m.person),
|
|
1618
|
+
invite: this.inviteOf(m),
|
|
1563
1619
|
role: m.role,
|
|
1564
1620
|
hasLeft: Boolean(m.leftAt)
|
|
1565
1621
|
};
|
|
1566
1622
|
},
|
|
1623
|
+
/** `pending` while an invited person has not answered, `declined` once they said no, else null. */
|
|
1624
|
+
inviteOf(m) {
|
|
1625
|
+
if (m.declinedAt) return INVITE.DECLINED;
|
|
1626
|
+
return m.person && !m.joinedAt ? INVITE.PENDING : null;
|
|
1627
|
+
},
|
|
1628
|
+
/**
|
|
1629
|
+
* An invitation as the invited person sees it: enough to decide, nothing of the books.
|
|
1630
|
+
* @param {Object} seat member row, `group` and `invitedBy` populated, with a `memberCount`
|
|
1631
|
+
*/
|
|
1632
|
+
invitation(seat) {
|
|
1633
|
+
return {
|
|
1634
|
+
id: seat.documentId,
|
|
1635
|
+
group: { id: seat.group.documentId, name: seat.group.name },
|
|
1636
|
+
invitedBy: seat.invitedBy ? { displayName: seat.invitedBy.displayName ?? null } : null,
|
|
1637
|
+
memberCount: seat.memberCount,
|
|
1638
|
+
invitedAt: iso$1(seat.createdAt)
|
|
1639
|
+
};
|
|
1640
|
+
},
|
|
1567
1641
|
/** A seat as an administrator sees it: the phone masked. */
|
|
1568
1642
|
adminMember(m) {
|
|
1569
1643
|
return { ...this.member(m), phone: maskPhone(this.phoneOf(m)), joinedAt: iso$1(m.joinedAt) };
|
|
@@ -1677,7 +1751,8 @@ const person = ({ strapi }) => ({
|
|
|
1677
1751
|
}
|
|
1678
1752
|
},
|
|
1679
1753
|
/**
|
|
1680
|
-
* Seats added by phone before this person existed become theirs
|
|
1754
|
+
* Seats added by phone before this person existed become invitations to them: theirs to accept
|
|
1755
|
+
* or decline, like any other.
|
|
1681
1756
|
* @param {Object} person
|
|
1682
1757
|
* @returns {Promise<number>} seats claimed
|
|
1683
1758
|
*/
|
|
@@ -1688,10 +1763,24 @@ const person = ({ strapi }) => ({
|
|
|
1688
1763
|
select: ["id"]
|
|
1689
1764
|
});
|
|
1690
1765
|
for (const seat of seats) {
|
|
1691
|
-
await strapi.db.query(UID.MEMBER).update({ where: { id: seat.id }, data: { person: person2.id
|
|
1766
|
+
await strapi.db.query(UID.MEMBER).update({ where: { id: seat.id }, data: { person: person2.id } });
|
|
1692
1767
|
}
|
|
1693
1768
|
return seats.length;
|
|
1694
1769
|
},
|
|
1770
|
+
/**
|
|
1771
|
+
* What a person chose for themselves.
|
|
1772
|
+
* @param {Object} person
|
|
1773
|
+
* @param {{ autoAcceptInvites?: boolean }} input
|
|
1774
|
+
* @returns {Promise<Object>} the person, updated
|
|
1775
|
+
*/
|
|
1776
|
+
async setPreferences(person2, input) {
|
|
1777
|
+
if (typeof input?.autoAcceptInvites !== "boolean")
|
|
1778
|
+
throw badRequest(ERROR_CODES.VALIDATION, "Préférence invalide.");
|
|
1779
|
+
return strapi.db.query(UID.PERSON).update({
|
|
1780
|
+
where: { id: person2.id },
|
|
1781
|
+
data: { autoAcceptInvites: input.autoAcceptInvites }
|
|
1782
|
+
});
|
|
1783
|
+
},
|
|
1695
1784
|
/** The registered person with this phone, if any. */
|
|
1696
1785
|
async byPhone(phone) {
|
|
1697
1786
|
return phone ? strapi.db.query(UID.PERSON).findOne({ where: { phone } }) : null;
|
|
@@ -1846,6 +1935,7 @@ const ledger = ({ strapi }) => ({
|
|
|
1846
1935
|
return books.settlements.filter((s) => s.status === SETTLEMENT_STATUS.PENDING && s.from === from && s.to === to).reduce((sum, s) => sum + s.amount, 0);
|
|
1847
1936
|
}
|
|
1848
1937
|
});
|
|
1938
|
+
const HELD = Object.freeze({ leftAt: { $null: true }, joinedAt: { $notNull: true } });
|
|
1849
1939
|
const cleanName = (raw) => String(raw ?? "").trim().replace(/\s+/g, " ").slice(0, NAME_MAX);
|
|
1850
1940
|
const group = ({ strapi }) => {
|
|
1851
1941
|
const svc = (name) => strapi.plugin(PLUGIN_ID).service(name);
|
|
@@ -1872,7 +1962,7 @@ const group = ({ strapi }) => {
|
|
|
1872
1962
|
async seat(person2, documentId, { writable = false } = {}) {
|
|
1873
1963
|
const group2 = await groups().findOne({ where: { documentId: String(documentId) } });
|
|
1874
1964
|
const me = group2 && await members().findOne({
|
|
1875
|
-
where: { group: group2.id, person: person2.id,
|
|
1965
|
+
where: { group: group2.id, person: person2.id, ...HELD },
|
|
1876
1966
|
populate: { person: true }
|
|
1877
1967
|
});
|
|
1878
1968
|
if (!me) throw notFound(ERROR_CODES.GROUP_NOT_FOUND, "Groupe introuvable.");
|
|
@@ -1888,7 +1978,7 @@ const group = ({ strapi }) => {
|
|
|
1888
1978
|
/** The seats a person holds, groups populated; archived groups only on request. */
|
|
1889
1979
|
async seatsOf(person2, { archived = false } = {}) {
|
|
1890
1980
|
const rows = await members().findMany({
|
|
1891
|
-
where: { person: person2.id,
|
|
1981
|
+
where: { person: person2.id, ...HELD },
|
|
1892
1982
|
populate: { group: true, person: true }
|
|
1893
1983
|
});
|
|
1894
1984
|
return rows.filter((m) => m.group && Boolean(m.group.archivedAt) === archived);
|
|
@@ -1905,7 +1995,7 @@ const group = ({ strapi }) => {
|
|
|
1905
1995
|
const name = cleanName(input.name);
|
|
1906
1996
|
if (!name) throw badRequest(ERROR_CODES.VALIDATION, "Donne un nom au groupe.");
|
|
1907
1997
|
const limits = svc("settings").limits();
|
|
1908
|
-
const held = await members().count({ where: { person: person2.id,
|
|
1998
|
+
const held = await members().count({ where: { person: person2.id, ...HELD } });
|
|
1909
1999
|
if (held >= limits.maxGroupsPerPerson)
|
|
1910
2000
|
throw unprocessable(
|
|
1911
2001
|
ERROR_CODES.GROUP_LIMIT,
|
|
@@ -1933,7 +2023,7 @@ const group = ({ strapi }) => {
|
|
|
1933
2023
|
joinedAt: now
|
|
1934
2024
|
}
|
|
1935
2025
|
});
|
|
1936
|
-
for (const other of others) await svc("member").seatFor(g, other);
|
|
2026
|
+
for (const other of others) await svc("member").seatFor(g, other, person2);
|
|
1937
2027
|
return g;
|
|
1938
2028
|
});
|
|
1939
2029
|
await svc("audit").log({
|
|
@@ -1968,8 +2058,8 @@ const group = ({ strapi }) => {
|
|
|
1968
2058
|
return updated;
|
|
1969
2059
|
},
|
|
1970
2060
|
/**
|
|
1971
|
-
* Take a seat with an invite code.
|
|
1972
|
-
*
|
|
2061
|
+
* Take a seat with an invite code. Entering the code of a group one was invited to accepts the
|
|
2062
|
+
* invitation; joining twice is not an error.
|
|
1973
2063
|
*/
|
|
1974
2064
|
async join(person2, rawCode) {
|
|
1975
2065
|
const inviteCode = String(rawCode ?? "").trim().toUpperCase();
|
|
@@ -1979,6 +2069,7 @@ const group = ({ strapi }) => {
|
|
|
1979
2069
|
const existing = await members().findOne({
|
|
1980
2070
|
where: { group: group2.id, person: person2.id, leftAt: { $null: true } }
|
|
1981
2071
|
});
|
|
2072
|
+
if (existing && !existing.joinedAt) await svc("member").take({ ...existing, group: group2 }, person2);
|
|
1982
2073
|
if (existing) return group2;
|
|
1983
2074
|
const limits = svc("settings").limits();
|
|
1984
2075
|
const count = await members().count({ where: { group: group2.id, leftAt: { $null: true } } });
|
|
@@ -2051,8 +2142,14 @@ const member = ({ strapi }) => {
|
|
|
2051
2142
|
throw conflict(ERROR_CODES.MEMBER_EXISTS, "Un numéro apparaît deux fois.");
|
|
2052
2143
|
return parsed;
|
|
2053
2144
|
},
|
|
2054
|
-
/**
|
|
2055
|
-
|
|
2145
|
+
/**
|
|
2146
|
+
* Create the seat. When that phone already uses the app it is an invitation to them, taken at
|
|
2147
|
+
* once only for someone who accepts every invitation.
|
|
2148
|
+
* @param {Object} group
|
|
2149
|
+
* @param {{ displayName: string, phone: string|null }} input
|
|
2150
|
+
* @param {Object} inviter the person adding them
|
|
2151
|
+
*/
|
|
2152
|
+
async seatFor(group2, { displayName, phone }, inviter) {
|
|
2056
2153
|
const person2 = await svc("person").byPhone(phone);
|
|
2057
2154
|
return members().create({
|
|
2058
2155
|
data: {
|
|
@@ -2061,10 +2158,20 @@ const member = ({ strapi }) => {
|
|
|
2061
2158
|
displayName,
|
|
2062
2159
|
phone,
|
|
2063
2160
|
role: ROLE.MEMBER,
|
|
2064
|
-
|
|
2161
|
+
invitedBy: inviter?.id ?? null,
|
|
2162
|
+
joinedAt: person2?.autoAcceptInvites ? /* @__PURE__ */ new Date() : null
|
|
2065
2163
|
}
|
|
2066
2164
|
});
|
|
2067
2165
|
},
|
|
2166
|
+
/** Is someone sitting here: a person who is in the group, not a name or an open invitation? */
|
|
2167
|
+
isHeld(seat) {
|
|
2168
|
+
return Boolean(seat.person) && Boolean(seat.joinedAt);
|
|
2169
|
+
},
|
|
2170
|
+
/** Where an invitation stands, or null for a seat that is held or was never anyone's. */
|
|
2171
|
+
inviteOf(seat) {
|
|
2172
|
+
if (seat.declinedAt) return INVITE.DECLINED;
|
|
2173
|
+
return seat.person && !seat.joinedAt ? INVITE.PENDING : null;
|
|
2174
|
+
},
|
|
2068
2175
|
/**
|
|
2069
2176
|
* @param {{ group: Object, me: Object }} seat the caller's
|
|
2070
2177
|
* @param {{ name: string, phone?: string }} raw
|
|
@@ -2085,7 +2192,7 @@ const member = ({ strapi }) => {
|
|
|
2085
2192
|
const format2 = svc("format");
|
|
2086
2193
|
if (input.phone && seats.some((s) => format2.phoneOf(s) === input.phone))
|
|
2087
2194
|
throw conflict(ERROR_CODES.MEMBER_EXISTS, "Ce numéro est déjà dans le groupe.");
|
|
2088
|
-
const created = await this.seatFor(group2, input);
|
|
2195
|
+
const created = await this.seatFor(group2, input, me.person);
|
|
2089
2196
|
await svc("audit").log({
|
|
2090
2197
|
group: group2,
|
|
2091
2198
|
actor: me.person,
|
|
@@ -2103,17 +2210,23 @@ const member = ({ strapi }) => {
|
|
|
2103
2210
|
if (!seat) throw notFound(ERROR_CODES.MEMBER_NOT_FOUND, "Membre introuvable.");
|
|
2104
2211
|
return seat;
|
|
2105
2212
|
},
|
|
2106
|
-
/**
|
|
2107
|
-
async
|
|
2213
|
+
/** What a seat still has on the books: its balance, and whether a payment awaits an answer. */
|
|
2214
|
+
async standing(group2, seat) {
|
|
2108
2215
|
const books = await svc("ledger").of(group2);
|
|
2109
2216
|
const busy = books.settlements.some(
|
|
2110
2217
|
(s) => s.status === SETTLEMENT_STATUS.PENDING && [s.from, s.to].includes(seat.id)
|
|
2111
2218
|
);
|
|
2112
|
-
|
|
2219
|
+
const balance = books.balanceOf(seat.id);
|
|
2220
|
+
return { balance, settled: balance === 0 && !busy };
|
|
2221
|
+
},
|
|
2222
|
+
/** A seat is only vacated once it is even and nothing awaits confirmation on it. */
|
|
2223
|
+
async assertSettled(group2, seat) {
|
|
2224
|
+
const { balance, settled } = await this.standing(group2, seat);
|
|
2225
|
+
if (!settled)
|
|
2113
2226
|
throw unprocessable(
|
|
2114
2227
|
ERROR_CODES.MEMBER_NOT_SETTLED,
|
|
2115
2228
|
"Les comptes de ce membre ne sont pas à zéro.",
|
|
2116
|
-
{ balance
|
|
2229
|
+
{ balance }
|
|
2117
2230
|
);
|
|
2118
2231
|
},
|
|
2119
2232
|
/** The owner removes someone else. */
|
|
@@ -2147,6 +2260,78 @@ const member = ({ strapi }) => {
|
|
|
2147
2260
|
/** Is this seat held by this person? */
|
|
2148
2261
|
isHeldBy(seat, person2) {
|
|
2149
2262
|
return idOf(seat.person) === person2.id;
|
|
2263
|
+
},
|
|
2264
|
+
/** The invitations a person has not answered, newest first; archived groups are not offered. */
|
|
2265
|
+
async invitations(person2) {
|
|
2266
|
+
const rows = await members().findMany({
|
|
2267
|
+
where: { person: person2.id, joinedAt: { $null: true }, leftAt: { $null: true } },
|
|
2268
|
+
populate: { group: true, invitedBy: true },
|
|
2269
|
+
orderBy: { id: "desc" }
|
|
2270
|
+
});
|
|
2271
|
+
const open = rows.filter((seat) => seat.group && !seat.group.archivedAt);
|
|
2272
|
+
return Promise.all(
|
|
2273
|
+
open.map(async (seat) => ({
|
|
2274
|
+
...seat,
|
|
2275
|
+
memberCount: await members().count({
|
|
2276
|
+
where: { group: seat.group.id, leftAt: { $null: true } }
|
|
2277
|
+
})
|
|
2278
|
+
}))
|
|
2279
|
+
);
|
|
2280
|
+
},
|
|
2281
|
+
/** One open invitation of this person, by the seat's document id. */
|
|
2282
|
+
async invitation(person2, documentId) {
|
|
2283
|
+
const seat = await members().findOne({
|
|
2284
|
+
where: {
|
|
2285
|
+
documentId: String(documentId),
|
|
2286
|
+
person: person2.id,
|
|
2287
|
+
joinedAt: { $null: true },
|
|
2288
|
+
leftAt: { $null: true }
|
|
2289
|
+
},
|
|
2290
|
+
populate: { group: true, person: true }
|
|
2291
|
+
});
|
|
2292
|
+
if (!seat?.group)
|
|
2293
|
+
throw notFound(ERROR_CODES.INVITATION_NOT_FOUND, "Cette invitation n'existe plus.");
|
|
2294
|
+
return seat;
|
|
2295
|
+
},
|
|
2296
|
+
/** The person takes the seat they were offered. */
|
|
2297
|
+
async take(seat, person2) {
|
|
2298
|
+
await members().update({ where: { id: seat.id }, data: { joinedAt: /* @__PURE__ */ new Date() } });
|
|
2299
|
+
await svc("audit").log({
|
|
2300
|
+
group: seat.group,
|
|
2301
|
+
actor: person2,
|
|
2302
|
+
action: EVENT.MEMBER_JOINED,
|
|
2303
|
+
targetId: seat.documentId
|
|
2304
|
+
});
|
|
2305
|
+
},
|
|
2306
|
+
/**
|
|
2307
|
+
* @param {Object} person
|
|
2308
|
+
* @param {string} documentId the invitation's
|
|
2309
|
+
* @returns {Promise<Object>} the group they are now in
|
|
2310
|
+
*/
|
|
2311
|
+
async accept(person2, documentId) {
|
|
2312
|
+
const seat = await this.invitation(person2, documentId);
|
|
2313
|
+
await this.take(seat, person2);
|
|
2314
|
+
return seat.group;
|
|
2315
|
+
},
|
|
2316
|
+
/**
|
|
2317
|
+
* The person says no. A seat with nothing on the books goes; one the group already counted in
|
|
2318
|
+
* its expenses stays as a name nobody holds, so the books still add up.
|
|
2319
|
+
* @param {Object} person
|
|
2320
|
+
* @param {string} documentId the invitation's
|
|
2321
|
+
*/
|
|
2322
|
+
async decline(person2, documentId) {
|
|
2323
|
+
const seat = await this.invitation(person2, documentId);
|
|
2324
|
+
const now = /* @__PURE__ */ new Date();
|
|
2325
|
+
const { settled } = await this.standing(seat.group, seat);
|
|
2326
|
+
const data = settled ? { leftAt: now, declinedAt: now } : { person: null, declinedAt: now };
|
|
2327
|
+
await members().update({ where: { id: seat.id }, data });
|
|
2328
|
+
await svc("audit").log({
|
|
2329
|
+
group: seat.group,
|
|
2330
|
+
actor: person2,
|
|
2331
|
+
action: EVENT.MEMBER_DECLINED,
|
|
2332
|
+
targetId: seat.documentId,
|
|
2333
|
+
data: { kept: !settled }
|
|
2334
|
+
});
|
|
2150
2335
|
}
|
|
2151
2336
|
};
|
|
2152
2337
|
};
|
|
@@ -2393,7 +2578,7 @@ const settlement = ({ strapi }) => {
|
|
|
2393
2578
|
to,
|
|
2394
2579
|
received,
|
|
2395
2580
|
method: String(input.method),
|
|
2396
|
-
instant: received || method.instant || !to
|
|
2581
|
+
instant: received || method.instant || !svc("member").isHeld(to),
|
|
2397
2582
|
reference: text(input?.reference, REF_MAX),
|
|
2398
2583
|
note: text(input?.note, NOTE_MAX),
|
|
2399
2584
|
clientRef: text(input?.clientRef, REF_MAX)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sonatel-os/strapi-plugin-split",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.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>",
|