Skip to content

Commit 4c502c7

Browse files
committed
chore: envelope logic works
1 parent a34ee4f commit 4c502c7

File tree

2 files changed

+65
-35
lines changed

2 files changed

+65
-35
lines changed

infrastructure/evault-core/src/db/db.service.spec.ts

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ import { it, describe, beforeAll, afterAll, expect } from "vitest";
44
import { v4 as uuidv4 } from "uuid";
55
import { Neo4jContainer, StartedNeo4jContainer } from "@testcontainers/neo4j";
66

7+
type Envelope = {
8+
id: string;
9+
ontology: string;
10+
value: any;
11+
};
12+
713
describe("DbService (integration)", () => {
814
let container: StartedNeo4jContainer;
915
let service: DbService;
@@ -28,42 +34,48 @@ describe("DbService (integration)", () => {
2834
});
2935

3036
it("should store and retrieve a meta-envelope", async () => {
31-
const testId = uuidv4();
3237
const input = {
33-
id: testId,
3438
ontology: "SocialMediaPost",
3539
payload: {
3640
text: "hello world",
3741
dateCreated: "2025-04-10",
3842
likes: ["user1", "user2"],
3943
},
44+
acl: ["@test-user"],
4045
};
4146

42-
const result = await service.storeMetaEnvelope(input, ["@test-user"]);
47+
const result = await service.storeMetaEnvelope(input, input.acl);
4348
const id = result.metaEnvelope.id;
4449

4550
const fetched = await service.findMetaEnvelopeById(id);
4651
expect(fetched).toBeDefined();
4752
expect(fetched.id).toBeDefined();
4853
expect(fetched.ontology).toBe("SocialMediaPost");
54+
expect(fetched.acl).toEqual(["@test-user"]);
4955
expect(fetched.envelopes).toHaveLength(3);
56+
expect(fetched.envelopes[0]).toEqual(
57+
expect.objectContaining({
58+
id: expect.any(String),
59+
ontology: expect.any(String),
60+
value: expect.any(String),
61+
}),
62+
);
63+
64+
console.log(fetched);
5065
});
5166

5267
it("should find meta-envelopes containing the search term in any envelope value", async () => {
53-
const metaId = uuidv4();
5468
const input = {
55-
id: metaId,
5669
ontology: "SocialMediaPost",
5770
payload: {
5871
text: "This is a searchable tweet",
5972
image: "https://example.com/image.jpg",
6073
likes: ["user1", "user2"],
6174
},
75+
acl: ["@search-test-user"],
6276
};
6377

64-
const metaEnv = await service.storeMetaEnvelope(input, [
65-
"@search-test-user",
66-
]);
78+
const metaEnv = await service.storeMetaEnvelope(input, input.acl);
6779

6880
const found = await service.findMetaEnvelopesBySearchTerm(
6981
"SocialMediaPost",
@@ -97,52 +109,49 @@ describe("DbService (integration)", () => {
97109
});
98110

99111
it("should delete a meta-envelope and its envelopes", async () => {
100-
const tempId = uuidv4();
101112
const meta = {
102-
id: tempId,
103113
ontology: "TempPost",
104114
payload: {
105115
value: "to be deleted",
106116
},
117+
acl: ["@delete-user"],
107118
};
108119

109-
await service.storeMetaEnvelope(meta, ["@delete-user"]);
110-
await service.deleteMetaEnvelope(tempId);
120+
const stored = await service.storeMetaEnvelope(meta, meta.acl);
121+
await service.deleteMetaEnvelope(stored.metaEnvelope.id);
111122

112-
const deleted = await service.findMetaEnvelopeById(tempId);
123+
const deleted = await service.findMetaEnvelopeById(
124+
stored.metaEnvelope.id,
125+
);
113126
expect(deleted).toBeNull();
114127
});
115128

116129
it("should update envelope value", async () => {
117-
const testId = uuidv4();
118130
const meta = {
119-
id: testId,
120131
ontology: "UpdateTest",
121132
payload: {
122133
value: "original",
123134
},
135+
acl: ["@updater"],
124136
};
125137

126-
const stored = await service.storeMetaEnvelope(meta, ["@updater"]);
138+
const stored = await service.storeMetaEnvelope(meta, meta.acl);
127139

128140
const result = await service.findMetaEnvelopeById(
129141
stored.metaEnvelope.id,
130142
);
131143
const targetEnvelope = result.envelopes.find(
132-
(e: any) => e.properties.ontology === "value",
144+
(e: Envelope) => e.ontology === "value",
133145
);
134146

135-
await service.updateEnvelopeValue(
136-
targetEnvelope.properties.id,
137-
"updated",
138-
);
147+
await service.updateEnvelopeValue(targetEnvelope.id, "updated");
139148

140149
const updated = await service.findMetaEnvelopeById(
141150
stored.metaEnvelope.id,
142151
);
143152
const updatedValue = updated.envelopes.find(
144-
(e: any) => e.properties.id === targetEnvelope.properties.id,
153+
(e: Envelope) => e.id === targetEnvelope.id,
145154
);
146-
expect(updatedValue.properties.value).toBe("updated");
155+
expect(updatedValue.value).toBe("updated");
147156
});
148157
});

infrastructure/evault-core/src/db/db.service.ts

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,15 @@ import neo4j, { Driver, Session } from "neo4j-driver";
22
import { W3IDBuilder } from "w3id";
33

44
type MetaEnvelope = {
5-
id: string;
65
ontology: string;
76
payload: Record<string, any>;
7+
acl: string[];
88
};
99

1010
type Envelope = {
1111
id: string;
1212
value: any;
1313
ontology: string;
14-
acl: string[];
1514
};
1615

1716
export class DbService {
@@ -30,17 +29,17 @@ export class DbService {
3029
}
3130
}
3231

33-
async storeMetaEnvelope(meta: MetaEnvelope, acl: string[]) {
32+
async storeMetaEnvelope(meta: Omit<MetaEnvelope, "id">, acl: string[]) {
3433
const w3id = await new W3IDBuilder().build();
3534

3635
const cypher: string[] = [
37-
`CREATE (m:MetaEnvelope { id: $metaId, ontology: $ontology })`,
36+
`CREATE (m:MetaEnvelope { id: $metaId, ontology: $ontology, acl: $acl })`,
3837
];
3938

4039
const envelopeParams: Record<string, any> = {
4140
metaId: w3id.id,
4241
ontology: meta.ontology,
43-
acl,
42+
acl: acl,
4443
};
4544

4645
const createdEnvelopes: Envelope[] = [];
@@ -58,8 +57,7 @@ export class DbService {
5857
CREATE (${alias}:Envelope {
5958
id: $${alias}_id,
6059
ontology: $${alias}_ontology,
61-
value: $${alias}_value,
62-
acl: $acl
60+
value: $${alias}_value
6361
})
6462
WITH m, ${alias}
6563
MERGE (m)-[:LINKS_TO]->(${alias})
@@ -73,7 +71,6 @@ export class DbService {
7371
id: envelopeId,
7472
ontology: key,
7573
value: value,
76-
acl,
7774
});
7875

7976
counter++;
@@ -85,6 +82,7 @@ export class DbService {
8582
metaEnvelope: {
8683
id: w3id.id,
8784
ontology: meta.ontology,
85+
acl: acl,
8886
},
8987
envelopes: createdEnvelopes,
9088
};
@@ -105,20 +103,36 @@ export class DbService {
105103

106104
return result.records.map((record) => ({
107105
id: record.get("id"),
108-
envelopes: record.get("envelopes").map((e: any) => e.properties),
106+
envelopes: record.get("envelopes").map((node: any) => ({
107+
id: node.properties.id,
108+
ontology: node.properties.ontology,
109+
value: node.properties.value,
110+
})),
109111
}));
110112
}
111113

112114
async findMetaEnvelopeById(id: string): Promise<any> {
113115
const result = await this.runQuery(
114116
`
115117
MATCH (m:MetaEnvelope { id: $id })-[:LINKS_TO]->(e:Envelope)
116-
RETURN m.id AS id, m.ontology AS ontology, collect(e) AS envelopes
118+
RETURN m.id AS id, m.ontology AS ontology, m.acl AS acl, collect(e) AS envelopes
117119
`,
118120
{ id },
119121
);
120122

121-
return result.records[0]?.toObject() ?? null;
123+
if (!result.records[0]) return null;
124+
125+
const record = result.records[0];
126+
return {
127+
id: record.get("id"),
128+
ontology: record.get("ontology"),
129+
acl: record.get("acl"),
130+
envelopes: record.get("envelopes").map((node: any) => ({
131+
id: node.properties.id,
132+
ontology: node.properties.ontology,
133+
value: node.properties.value,
134+
})),
135+
};
122136
}
123137

124138
async findMetaEnvelopesByOntology(ontology: string): Promise<string[]> {
@@ -158,7 +172,14 @@ export class DbService {
158172

159173
async getAllEnvelopes(): Promise<Envelope[]> {
160174
const result = await this.runQuery(`MATCH (e:Envelope) RETURN e`, {});
161-
return result.records.map((r) => r.get("e").properties as Envelope);
175+
return result.records.map((r) => {
176+
const node = r.get("e");
177+
return {
178+
id: node.properties.id,
179+
ontology: node.properties.ontology,
180+
value: node.properties.value,
181+
};
182+
});
162183
}
163184

164185
async close(): Promise<void> {

0 commit comments

Comments
 (0)