@fonderie/customers 6.1.4 → 6.1.6

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/dist/index.cjs CHANGED
@@ -1078,11 +1078,23 @@ var CustomerLabelModel = class {
1078
1078
  [type]
1079
1079
  );
1080
1080
  }
1081
- remove(id) {
1082
- return this.store.query(
1083
- `DELETE FROM fonderie_customer_labels WHERE id = $1`,
1081
+ /**
1082
+ * Returns false when the label is still referenced. Labels are a SHARED
1083
+ * table (no workspace column), so an unconditional delete would let one
1084
+ * tenant destroy a label other tenants' emails/phones/addresses point at.
1085
+ * Until labels are workspace-scoped, only an unreferenced label may go.
1086
+ */
1087
+ async remove(id) {
1088
+ const rows = await this.store.query(
1089
+ `DELETE FROM fonderie_customer_labels l
1090
+ WHERE l.id = $1
1091
+ AND NOT EXISTS (SELECT 1 FROM fonderie_customer_emails WHERE label_id = l.id)
1092
+ AND NOT EXISTS (SELECT 1 FROM fonderie_customer_phones WHERE label_id = l.id)
1093
+ AND NOT EXISTS (SELECT 1 FROM fonderie_customer_addresses WHERE label_id = l.id)
1094
+ RETURNING l.id`,
1084
1095
  [id]
1085
- ).then(() => void 0);
1096
+ );
1097
+ return rows.length > 0;
1086
1098
  }
1087
1099
  async findOrCreate(type, raw) {
1088
1100
  const value = raw.trim().toLowerCase();
@@ -2195,7 +2207,14 @@ function customerLabelController(store) {
2195
2207
  if (!isUuid(labelId)) {
2196
2208
  return (0, import_core8.setApiResponse)(import_core8.HTTP.UNPROCESSABLE, "INVALID_PARAMETER", "labelId must be a valid UUID");
2197
2209
  }
2198
- await labels.remove(labelId);
2210
+ const removed = await labels.remove(labelId);
2211
+ if (!removed) {
2212
+ return (0, import_core8.setApiResponse)(
2213
+ import_core8.HTTP.CONFLICT,
2214
+ "LABEL_IN_USE",
2215
+ "Label is still referenced by customer records and cannot be deleted."
2216
+ );
2217
+ }
2199
2218
  return (0, import_core8.setApiResponse)(import_core8.HTTP.OK, "LABEL_DELETED", "Label deleted successfully.");
2200
2219
  }
2201
2220
  };