oficina / base Public

Clone
git clone https://amadan.net/oficina/base

Plain git — no account needed to clone.

Download

Download this file

1package store
2
3import (
4 "context"
5 "database/sql"
6)
7
8// RemoveMember removes the member with id and, in the same transaction, the
9// Oficina ID links that name its number, as connect requires: a link left
10// behind would hold its Oficina account so it could not link again. The
11// number itself stays taken, so it can never name anyone else. With Oficina
12// ID off the links table may not exist, and there is nothing to delete.
13func RemoveMember(ctx context.Context, db *sql.DB, id string) error {
14 tx, err := db.BeginTx(ctx, nil)
15 if err != nil {
16 return err
17 }
18 defer tx.Rollback()
19 var links int
20 if err := tx.QueryRowContext(ctx, `SELECT count(*) FROM sqlite_master WHERE type = 'table' AND name = 'home_identity_links'`).Scan(&links); err != nil {
21 return err
22 }
23 if links > 0 {
24 if _, err := tx.ExecContext(ctx, `DELETE FROM home_identity_links
25 WHERE person_id IN (SELECT number FROM member_numbers WHERE member_id = ?)`, id); err != nil {
26 return err
27 }
28 }
29 if _, err := tx.ExecContext(ctx, `DELETE FROM members WHERE id = ?`, id); err != nil {
30 return err
31 }
32 return tx.Commit()
33}
34