You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Handle postgres data in update
* Maybe implement profile
* Test both backends
* Clean up some old queries
* Implement import, export, and delete-my-data
* Handle separate test import
* Fixes
* Compile error
* Forgot the migration
* Don't need this test anymore
Copy file name to clipboardExpand all lines: api/db/item-annotations-queries.ts
+75-28Lines changed: 75 additions & 28 deletions
Original file line number
Diff line number
Diff line change
@@ -1,3 +1,4 @@
1
+
import{partition}from'es-toolkit';
1
2
import{ClientBase,QueryResult}from'pg';
2
3
import{metrics}from'../metrics/index.js';
3
4
import{DestinyVersion}from'../shapes/general.js';
@@ -7,6 +8,7 @@ interface ItemAnnotationRow {
7
8
inventory_item_id: string;
8
9
tag: TagValue|null;
9
10
notes: string|null;
11
+
deleted_at: Date|null;
10
12
crafted_date: Date|null;
11
13
}
12
14
@@ -37,31 +39,26 @@ export async function getItemAnnotationsForProfile(
37
39
}
38
40
39
41
/**
40
-
* Get ALL of the item annotations for a particular user across all platforms.
42
+
* Get all of the item annotations for a particular platform_membership_id and destiny_version that have changed since the token timestamp, including all tombstones.
text: 'SELECT platform_membership_id, destiny_version, inventory_item_id, tag, notes, crafted_date FROM item_annotations WHERE inventory_item_id != 0 and platform_membership_id = $1 and deleted_at IS NULL',
values ($1, $2, $3, $4, (CASE WHEN $5 = 0 THEN NULL ELSE $5 END), (CASE WHEN $6 = 'clear' THEN NULL ELSE $6 END), $7)
103
-
on conflict (platform_membership_id, inventory_item_id)
104
-
do update set (tag, notes, crafted_date, deleted_at) = ((CASE WHEN $5 = 0 THEN NULL WHEN $5 IS NULL THEN item_annotations.tag ELSE $5 END), (CASE WHEN $6 = 'clear' THEN NULL WHEN $6 IS NULL THEN item_annotations.notes ELSE $6 END), $7, null)`,
98
+
text: `
99
+
INSERT INTO item_annotations (
100
+
membership_id,
101
+
platform_membership_id,
102
+
destiny_version,
103
+
inventory_item_id,
104
+
tag,
105
+
notes,
106
+
crafted_date
107
+
)
108
+
VALUES (
109
+
$1,
110
+
$2,
111
+
$3,
112
+
$4,
113
+
(CASE WHEN $5 = 0 THEN NULL ELSE $5 END),
114
+
(CASE WHEN $6 = 'clear' THEN NULL ELSE $6 END),
115
+
$7
116
+
)
117
+
ON CONFLICT (platform_membership_id, inventory_item_id)
118
+
DO UPDATE SET
119
+
tag = (CASE
120
+
WHEN $5 = 0 THEN NULL
121
+
WHEN $5 IS NULL THEN (CASE WHEN item_annotations.deleted_at IS NULL THEN item_annotations.tag ELSE NULL END)
122
+
ELSE $5
123
+
END),
124
+
notes = (CASE
125
+
WHEN $6 = 'clear' THEN NULL
126
+
WHEN $6 IS NULL THEN (CASE WHEN item_annotations.deleted_at IS NULL THEN item_annotations.notes ELSE NULL END)
127
+
ELSE $6
128
+
END),
129
+
crafted_date = $7,
130
+
deleted_at = (CASE
131
+
WHEN (CASE WHEN $5 = 0 THEN NULL WHEN $5 IS NULL THEN (CASE WHEN item_annotations.deleted_at IS NULL THEN item_annotations.tag ELSE NULL END) ELSE $5 END) IS NULL
132
+
AND (CASE WHEN $6 = 'clear' THEN NULL WHEN $6 IS NULL THEN (CASE WHEN item_annotations.deleted_at IS NULL THEN item_annotations.notes ELSE NULL END) ELSE $6 END) IS NULL
133
+
THEN now()
134
+
ELSE NULL
135
+
END)
136
+
`,
105
137
values: [
106
138
bungieMembershipId,// $1
107
139
platformMembershipId,// $2
@@ -147,7 +179,7 @@ export async function deleteItemAnnotation(
147
179
): Promise<QueryResult>{
148
180
returnclient.query({
149
181
name: 'delete_item_annotation',
150
-
text: `update item_annotations set (tag, notes, deleted_at) = (null, null, now()) where platform_membership_id = $1 and inventory_item_id = $2`,
182
+
text: `update item_annotations set deleted_at = now() where platform_membership_id = $1 and inventory_item_id = $2 and deleted_at is null`,
151
183
values: [platformMembershipId,inventoryItemId],
152
184
});
153
185
}
@@ -162,7 +194,7 @@ export async function deleteItemAnnotationList(
162
194
): Promise<QueryResult>{
163
195
returnclient.query({
164
196
name: 'delete_item_annotation_list',
165
-
text: `update item_annotations set (tag, notes, deleted_at) = (null, null, now()) where platform_membership_id = $1 and inventory_item_id::bigint = ANY($2::bigint[])`,
197
+
text: `update item_annotations set deleted_at = now() where platform_membership_id = $1 and inventory_item_id::bigint = ANY($2::bigint[]) and deleted_at is null`,
166
198
values: [platformMembershipId,inventoryItemIds],
167
199
});
168
200
}
@@ -181,3 +213,18 @@ export async function deleteAllItemAnnotations(
181
213
values: [bungieMembershipId],
182
214
});
183
215
}
216
+
217
+
/**
218
+
* Soft-delete all item annotations for a platform (sets deleted_at timestamp for sync support).
219
+
*/
220
+
exportasyncfunctionsoftDeleteAllItemAnnotations(
221
+
client: ClientBase,
222
+
platformMembershipId: string,
223
+
destinyVersion: DestinyVersion,
224
+
): Promise<QueryResult>{
225
+
returnclient.query({
226
+
name: 'soft_delete_all_item_annotations',
227
+
text: `update item_annotations set deleted_at = now() where platform_membership_id = $1 and destiny_version = $2 and deleted_at is null`,
@@ -55,10 +79,40 @@ export async function updateItemHashTag(
55
79
56
80
constresponse=awaitclient.query({
57
81
name: 'upsert_hash_tag',
58
-
text: `insert INTO item_hash_tags (membership_id, platform_membership_id, item_hash, tag, notes)
59
-
values ($1, $2, $3, (CASE WHEN $4 = 0 THEN NULL ELSE $4 END), (CASE WHEN $5 = 'clear' THEN NULL ELSE $5 END))
60
-
on conflict (platform_membership_id, item_hash)
61
-
do update set (tag, notes, deleted_at) = ((CASE WHEN $4 = 0 THEN NULL WHEN $4 IS NULL THEN item_hash_tags.tag ELSE $4 END), (CASE WHEN $5 = 'clear' THEN NULL WHEN $5 IS NULL THEN item_hash_tags.notes ELSE $5 END), null)`,
82
+
text: `
83
+
INSERT INTO item_hash_tags (
84
+
membership_id,
85
+
platform_membership_id,
86
+
item_hash,
87
+
tag,
88
+
notes
89
+
)
90
+
VALUES (
91
+
$1,
92
+
$2,
93
+
$3,
94
+
(CASE WHEN $4 = 0 THEN NULL ELSE $4 END),
95
+
(CASE WHEN $5 = 'clear' THEN NULL ELSE $5 END)
96
+
)
97
+
ON CONFLICT (platform_membership_id, item_hash)
98
+
DO UPDATE SET
99
+
tag = (CASE
100
+
WHEN $4 = 0 THEN NULL
101
+
WHEN $4 IS NULL THEN (CASE WHEN item_hash_tags.deleted_at IS NULL THEN item_hash_tags.tag ELSE NULL END)
102
+
ELSE $4
103
+
END),
104
+
notes = (CASE
105
+
WHEN $5 = 'clear' THEN NULL
106
+
WHEN $5 IS NULL THEN (CASE WHEN item_hash_tags.deleted_at IS NULL THEN item_hash_tags.notes ELSE NULL END)
107
+
ELSE $5
108
+
END),
109
+
deleted_at = (CASE
110
+
WHEN (CASE WHEN $4 = 0 THEN NULL WHEN $4 IS NULL THEN (CASE WHEN item_hash_tags.deleted_at IS NULL THEN item_hash_tags.tag ELSE NULL END) ELSE $4 END) IS NULL
111
+
AND (CASE WHEN $5 = 'clear' THEN NULL WHEN $5 IS NULL THEN (CASE WHEN item_hash_tags.deleted_at IS NULL THEN item_hash_tags.notes ELSE NULL END) ELSE $5 END) IS NULL
112
+
THEN now()
113
+
ELSE NULL
114
+
END)
115
+
`,
62
116
values: [
63
117
bungieMembershipId,
64
118
platformMembershipId,
@@ -102,7 +156,7 @@ export async function deleteItemHashTag(
102
156
): Promise<QueryResult>{
103
157
returnclient.query({
104
158
name: 'delete_item_hash_tag',
105
-
text: `update item_hash_tags set (tag, notes, deleted_at) = (null, null, now()) where platform_membership_id = $1 and item_hash = $2`,
159
+
text: `update item_hash_tags set deleted_at = now() where platform_membership_id = $1 and item_hash = $2 and deleted_at is null`,
106
160
values: [platformMembershipId,itemHash],
107
161
});
108
162
}
@@ -120,3 +174,17 @@ export async function deleteAllItemHashTags(
120
174
values: [platformMembershipId],
121
175
});
122
176
}
177
+
178
+
/**
179
+
* Soft-delete all item hash tags for a platform (sets deleted_at timestamp for sync support).
180
+
*/
181
+
exportasyncfunctionsoftDeleteAllItemHashTags(
182
+
client: ClientBase,
183
+
platformMembershipId: string,
184
+
): Promise<QueryResult>{
185
+
returnclient.query({
186
+
name: 'soft_delete_all_item_hash_tags',
187
+
text: `update item_hash_tags set deleted_at = now() where platform_membership_id = $1 and deleted_at is null`,
0 commit comments