| 1 | // Command preview serves the REAL app — real handlers, templates and |
| 2 | // store — over a throwaway database of synthetic customers, already signed |
| 3 | // in, on loopback. It exists so a screen can be looked at before it ships: |
| 4 | // the first index went live with a bullet point and an underlined link |
| 5 | // that no test could see. |
| 6 | // |
| 7 | // go run ./scripts/preview # then open http://127.0.0.1:8746/ |
| 8 | // |
| 9 | // Signed in through AppForTest, which says what it does in its name; this |
| 10 | // binary binds 127.0.0.1 only and refuses to run otherwise. |
| 11 | package main |
| 12 | |
| 13 | import ( |
| 14 | "context" |
| 15 | "crypto/rand" |
| 16 | "encoding/base64" |
| 17 | "fmt" |
| 18 | "io" |
| 19 | "log/slog" |
| 20 | "net/http" |
| 21 | "os" |
| 22 | "path/filepath" |
| 23 | "strconv" |
| 24 | |
| 25 | fichas "amadan.net/oficina/fichas/internal/fichas" |
| 26 | "amadan.net/rastrillo/rastrillo/db" |
| 27 | ) |
| 28 | |
| 29 | const ( |
| 30 | addr = "127.0.0.1:8746" |
| 31 | owner = "paul@example.com" |
| 32 | ) |
| 33 | |
| 34 | func main() { |
| 35 | dir, err := os.MkdirTemp("", "fichas-preview-") |
| 36 | check(err) |
| 37 | defer os.RemoveAll(dir) |
| 38 | |
| 39 | dek := make([]byte, 32) |
| 40 | _, err = rand.Read(dek) |
| 41 | check(err) |
| 42 | env := map[string]string{ |
| 43 | "FICHAS_DEK": base64.StdEncoding.EncodeToString(dek), |
| 44 | "FICHAS_INSTANCE_KEY": "preview", |
| 45 | "FICHAS_OWNERS": owner, |
| 46 | "FICHAS_ORIGIN": "http://" + addr, |
| 47 | "FICHAS_ALLOW_LOGGED_MAIL": "1", |
| 48 | } |
| 49 | cfg, err := fichas.LoadConfig(func(k string) string { return env[k] }) |
| 50 | check(err) |
| 51 | |
| 52 | logger := slog.New(slog.NewTextHandler(io.Discard, nil)) |
| 53 | d, err := db.Open(filepath.Join(dir, "preview.db"), logger) |
| 54 | check(err) |
| 55 | mux, err := fichas.AppForTest(d, cfg, logger, owner) |
| 56 | check(err) |
| 57 | check(seed(d, cfg)) |
| 58 | |
| 59 | fmt.Println("fichas preview on http://" + addr + "/ (synthetic data, signed in as " + owner + ")") |
| 60 | check(http.ListenAndServe(addr, mux)) |
| 61 | } |
| 62 | |
| 63 | // seed writes synthetic customers through the real store, so the preview |
| 64 | // exercises the same write path — events and all — that production does. |
| 65 | // .example is RFC 2606's reserved TLD. |
| 66 | func seed(d *db.DB, cfg fichas.Config) error { |
| 67 | ctx := context.Background() |
| 68 | m, err := fichas.EnsureMember(ctx, d.G, cfg.Sealer, owner) |
| 69 | if err != nil { |
| 70 | return err |
| 71 | } |
| 72 | me := fichas.Person(m.ID) |
| 73 | customers := []struct { |
| 74 | values map[string]string |
| 75 | calls []string |
| 76 | }{ |
| 77 | {map[string]string{fichas.FieldName: "Brightwater Labs", fichas.FieldDomain: "brightwater.example", |
| 78 | fichas.FieldEmail: "ana@brightwater.example", fichas.FieldPhone: "+353 1 555 0142", |
| 79 | fichas.FieldStage: "Proposal", fichas.FieldNextAction: "2026-10-02", |
| 80 | fichas.FieldNotes: "Renewal is tied to their grant cycle. Ana is the budget holder; Tom signs."}, |
| 81 | []string{"Met at the conference. Asked for a follow-up after their board meets.", |
| 82 | "Intro call. Current tool is a shared spreadsheet; they lose track of follow-ups.", |
| 83 | "Walked Ana through the proposal. She wants the pilot scoped to one department first."}}, |
| 84 | {map[string]string{fichas.FieldName: "Cedar & Pike Architects", fichas.FieldDomain: "cedarpike.example", |
| 85 | fichas.FieldEmail: "office@cedarpike.example", fichas.FieldStage: "Lead"}, nil}, |
| 86 | {map[string]string{fichas.FieldName: "Halden Robotics", fichas.FieldDomain: "halden.example", |
| 87 | fichas.FieldEmail: "ops@halden.example", fichas.FieldPhone: "+44 20 5550 1877", |
| 88 | fichas.FieldStage: "Contacted", fichas.FieldNextAction: "2026-09-30"}, |
| 89 | []string{"Left a voicemail; followed up by email."}}, |
| 90 | {map[string]string{fichas.FieldName: "Oriel Housing Trust", fichas.FieldDomain: "oriel.example", |
| 91 | fichas.FieldEmail: "it@oriel.example", fichas.FieldStage: "Won"}, |
| 92 | []string{"Signed. Rollout starts next month."}}, |
| 93 | } |
| 94 | // PREVIEW_EXTRA=5000 adds that many more synthetic customers, to look at |
| 95 | // and time the list at a realistic size. |
| 96 | if n, _ := strconv.Atoi(os.Getenv("PREVIEW_EXTRA")); n > 0 { |
| 97 | stages := []string{"Lead", "Contacted", "Proposal", "Won", "Lost"} |
| 98 | for i := 0; i < n; i++ { |
| 99 | customers = append(customers, struct { |
| 100 | values map[string]string |
| 101 | calls []string |
| 102 | }{map[string]string{ |
| 103 | fichas.FieldName: fmt.Sprintf("Company %04d", i), |
| 104 | fichas.FieldEmail: fmt.Sprintf("hello%04d@company%04d.example", i, i), |
| 105 | fichas.FieldStage: stages[i%5], |
| 106 | fichas.FieldNotes: "Renewal is tied to their grant cycle; the budget holder signs off in March.", |
| 107 | }, nil}) |
| 108 | } |
| 109 | } |
| 110 | for _, c := range customers { |
| 111 | card, err := fichas.CreateCard(ctx, d.G, cfg.Sealer, fichas.TypeCustomer, me, c.values) |
| 112 | if err != nil { |
| 113 | return err |
| 114 | } |
| 115 | for _, body := range c.calls { |
| 116 | if _, err := fichas.AddEntry(ctx, d.G, cfg.Sealer, card.ID, me, fichas.TypeCallLog, "manual", |
| 117 | map[string]string{fichas.FieldBody: body}); err != nil { |
| 118 | return err |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | return nil |
| 123 | } |
| 124 | |
| 125 | func check(err error) { |
| 126 | if err != nil { |
| 127 | fmt.Fprintln(os.Stderr, "preview:", err) |
| 128 | os.Exit(1) |
| 129 | } |
| 130 | } |
| 131 | |