Skip to content

Commit 21e87b3

Browse files
committed
feat: synchronization works perfectly
1 parent 0f86b61 commit 21e87b3

File tree

22 files changed

+277
-455
lines changed

22 files changed

+277
-455
lines changed

infrastructure/evault-core/src/protocol/graphql-server.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -170,11 +170,22 @@ export class GraphQLServer {
170170
schemaId: input.ontology,
171171
};
172172

173-
// Fire and forget webhook delivery
174-
this.deliverWebhooks(
175-
requestingPlatform,
176-
webhookPayload
177-
);
173+
/**
174+
* To whoever who reads this in the future please don't
175+
* remove this delay as this prevents a VERY horrible
176+
* disgusting edge case, where if a platform's URL is
177+
* not determinable the webhook to the same platform as
178+
* the one who sent off the request gets sent and that
179+
* is not an ideal case trust me I've suffered, it
180+
* causes an absolutely beautiful error where you get
181+
* stuck in what I like to call webhook ping-pong
182+
*/
183+
setTimeout(() => {
184+
this.deliverWebhooks(
185+
requestingPlatform,
186+
webhookPayload
187+
);
188+
}, 3_000);
178189

179190
return result;
180191
}

infrastructure/evault-core/src/protocol/vault-access-guard.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ export class VaultAccessGuard {
6060
context: VaultContext
6161
): Promise<boolean> {
6262
// Validate token if present
63-
const authHeader = context.request?.headers?.get("authorization");
63+
const authHeader =
64+
context.request?.headers?.get("authorization") ??
65+
context.request?.headers?.get("Authorization");
6466
const tokenPayload = await this.validateToken(authHeader);
6567

6668
if (tokenPayload) {

infrastructure/web3-adapter/src/db/mapping.db.ts

Lines changed: 40 additions & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -27,89 +27,76 @@ export class MappingDatabase {
2727
CREATE TABLE IF NOT EXISTS id_mappings (
2828
local_id TEXT NOT NULL,
2929
global_id TEXT NOT NULL,
30-
table_name TEXT NOT NULL,
3130
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
32-
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
33-
PRIMARY KEY (local_id, table_name),
34-
UNIQUE (global_id, table_name)
31+
PRIMARY KEY (global_id)
3532
)
3633
`);
3734

3835
await this.runAsync(`
39-
CREATE INDEX IF NOT EXISTS idx_global_id ON id_mappings(global_id)
40-
`);
41-
42-
await this.runAsync(`
43-
CREATE INDEX IF NOT EXISTS idx_table_name ON id_mappings(table_name)
36+
CREATE INDEX IF NOT EXISTS idx_local_id ON id_mappings(local_id)
4437
`);
4538
}
4639

4740
/**
48-
* Store a mapping between local and global IDs (UPSERT)
41+
* Store a mapping between local and global IDs
4942
*/
5043
public async storeMapping(params: {
5144
localId: string;
5245
globalId: string;
53-
tableName: string;
5446
}): Promise<void> {
5547
// Validate inputs
56-
if (!params.localId || !params.globalId || !params.tableName) {
48+
if (!params.localId || !params.globalId) {
5749
throw new Error(
58-
"Invalid mapping parameters: all fields are required",
50+
"Invalid mapping parameters: all fields are required"
5951
);
6052
}
6153

54+
console.log("storing mapping g:l", params.globalId, params.localId);
55+
56+
// Check if mapping already exists
57+
const existingMapping = await this.getGlobalId(params.localId);
58+
59+
if (existingMapping) {
60+
return;
61+
}
62+
6263
try {
63-
// Use UPSERT to handle both insert and update cases
6464
await this.runAsync(
65-
`INSERT OR REPLACE INTO id_mappings (local_id, global_id, table_name, updated_at)
66-
VALUES (?, ?, ?, CURRENT_TIMESTAMP)`,
67-
[params.localId, params.globalId, params.tableName],
65+
`INSERT INTO id_mappings (local_id, global_id)
66+
VALUES (?, ?)`,
67+
[params.localId, params.globalId]
6868
);
6969

70-
// Verify the mapping was stored correctly
71-
const storedMapping = await this.getGlobalId({
72-
localId: params.localId,
73-
tableName: params.tableName,
74-
});
70+
const storedMapping = await this.getGlobalId(params.localId);
7571

7672
if (storedMapping !== params.globalId) {
77-
console.error("Mapping verification failed:", {
78-
expected: params.globalId,
79-
actual: storedMapping,
80-
params
81-
});
82-
throw new Error("Failed to store mapping - verification failed");
73+
console.log(
74+
"storedMappingError",
75+
storedMapping,
76+
params.globalId
77+
);
78+
console.error("Failed to store mapping");
79+
return;
8380
}
84-
85-
console.log("Successfully stored mapping:", {
86-
localId: params.localId,
87-
globalId: params.globalId,
88-
tableName: params.tableName
89-
});
9081
} catch (error) {
91-
console.error("Error storing mapping:", error, params);
9282
throw error;
9383
}
9484
}
9585

9686
/**
9787
* Get the global ID for a local ID
9888
*/
99-
public async getGlobalId(params: {
100-
localId: string;
101-
tableName: string;
102-
}): Promise<string | null> {
103-
if (!params.localId || !params.tableName) {
89+
public async getGlobalId(localId: string): Promise<string | null> {
90+
if (!localId) {
10491
return null;
10592
}
10693

10794
try {
10895
const result = await this.getAsync(
10996
`SELECT global_id
11097
FROM id_mappings
111-
WHERE local_id = ? AND table_name = ?`,
112-
[params.localId, params.tableName],
98+
WHERE local_id = ?`,
99+
[localId]
113100
);
114101
return result?.global_id ?? null;
115102
} catch (error) {
@@ -121,20 +108,17 @@ export class MappingDatabase {
121108
/**
122109
* Get the local ID for a global ID
123110
*/
124-
public async getLocalId(params: {
125-
globalId: string;
126-
tableName: string;
127-
}): Promise<string | null> {
128-
if (!params.globalId || !params.tableName) {
111+
public async getLocalId(globalId: string): Promise<string | null> {
112+
if (!globalId) {
129113
return null;
130114
}
131115

132116
try {
133117
const result = await this.getAsync(
134118
`SELECT local_id
135119
FROM id_mappings
136-
WHERE global_id = ? AND table_name = ?`,
137-
[params.globalId, params.tableName],
120+
WHERE global_id = ?`,
121+
[globalId]
138122
);
139123
return result?.local_id ?? null;
140124
} catch (error) {
@@ -145,44 +129,35 @@ export class MappingDatabase {
145129
/**
146130
* Delete a mapping
147131
*/
148-
public async deleteMapping(params: {
149-
localId: string;
150-
tableName: string;
151-
}): Promise<void> {
152-
if (!params.localId || !params.tableName) {
132+
public async deleteMapping(localId: string): Promise<void> {
133+
if (!localId) {
153134
return;
154135
}
155136

156137
try {
157138
await this.runAsync(
158139
`DELETE FROM id_mappings
159-
WHERE local_id = ? AND table_name = ?`,
160-
[params.localId, params.tableName],
140+
WHERE local_id = ?`,
141+
[localId]
161142
);
162143
} catch (error) {
163144
throw error;
164145
}
165146
}
166147

167148
/**
168-
* Get all mappings for a table
149+
* Get all mappings
169150
*/
170-
public async getTableMappings(tableName: string): Promise<
151+
public async getAllMappings(): Promise<
171152
Array<{
172153
localId: string;
173154
globalId: string;
174155
}>
175156
> {
176-
if (!tableName) {
177-
return [];
178-
}
179-
180157
try {
181158
const results = await this.allAsync(
182159
`SELECT local_id, global_id
183-
FROM id_mappings
184-
WHERE table_name = ?`,
185-
[tableName],
160+
FROM id_mappings`
186161
);
187162

188163
return results.map(({ local_id, global_id }) => ({
@@ -194,83 +169,6 @@ export class MappingDatabase {
194169
}
195170
}
196171

197-
/**
198-
* Debug method to get all mappings
199-
*/
200-
public async getAllMappings(): Promise<Array<{
201-
localId: string;
202-
globalId: string;
203-
tableName: string;
204-
createdAt: string;
205-
updatedAt: string;
206-
}>> {
207-
try {
208-
const results = await this.allAsync(
209-
`SELECT local_id, global_id, table_name, created_at, updated_at
210-
FROM id_mappings
211-
ORDER BY table_name, local_id`
212-
);
213-
214-
return results.map(({ local_id, global_id, table_name, created_at, updated_at }) => ({
215-
localId: local_id,
216-
globalId: global_id,
217-
tableName: table_name,
218-
createdAt: created_at,
219-
updatedAt: updated_at,
220-
}));
221-
} catch (error) {
222-
console.error("Error getting all mappings:", error);
223-
return [];
224-
}
225-
}
226-
227-
/**
228-
* Debug method to check if a mapping exists
229-
*/
230-
public async debugMapping(params: {
231-
localId?: string;
232-
globalId?: string;
233-
tableName: string;
234-
}): Promise<{
235-
exists: boolean;
236-
mapping?: any;
237-
allMappingsForTable: Array<{ localId: string; globalId: string }>;
238-
}> {
239-
const { localId, globalId, tableName } = params;
240-
241-
try {
242-
let mapping = null;
243-
244-
if (localId) {
245-
const globalIdResult = await this.getGlobalId({ localId, tableName });
246-
if (globalIdResult) {
247-
mapping = { localId, globalId: globalIdResult, tableName };
248-
}
249-
}
250-
251-
if (globalId && !mapping) {
252-
const localIdResult = await this.getLocalId({ globalId, tableName });
253-
if (localIdResult) {
254-
mapping = { localId: localIdResult, globalId, tableName };
255-
}
256-
}
257-
258-
const allMappingsForTable = await this.getTableMappings(tableName);
259-
260-
return {
261-
exists: !!mapping,
262-
mapping,
263-
allMappingsForTable,
264-
};
265-
} catch (error) {
266-
console.error("Error debugging mapping:", error);
267-
return {
268-
exists: false,
269-
allMappingsForTable: [],
270-
};
271-
}
272-
}
273-
274172
/**
275173
* Close the database connection
276174
*/

infrastructure/web3-adapter/src/evault/evault.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,10 @@ export class EVaultClient {
112112
private async requestPlatformToken(): Promise<string> {
113113
try {
114114
const response = await axios.post<PlatformTokenResponse>(
115-
new URL("/platforms/certification", this.registryUrl).toString(),
115+
new URL(
116+
"/platforms/certification",
117+
this.registryUrl
118+
).toString(),
116119
{ platform: this.platform },
117120
{
118121
headers: {
@@ -141,7 +144,7 @@ export class EVaultClient {
141144
private async resolveEndpoint(w3id: string): Promise<string> {
142145
try {
143146
const response = await axios.get(
144-
new URL(`/resolve?w3id=${w3id}`, this.registryUrl).toString(),
147+
new URL(`/resolve?w3id=${w3id}`, this.registryUrl).toString()
145148
);
146149
return new URL("/graphql", response.data.uri).toString();
147150
} catch (error) {
@@ -154,7 +157,7 @@ export class EVaultClient {
154157
if (!this.endpoint || !this.client) {
155158
this.endpoint = await this.resolveEndpoint(w3id).catch(() => null);
156159
if (!this.endpoint) throw new Error();
157-
160+
158161
// Get platform token and create client with authorization header
159162
const token = await this.ensurePlatformToken();
160163
this.client = new GraphQLClient(this.endpoint, {
@@ -171,7 +174,8 @@ export class EVaultClient {
171174
return null;
172175
});
173176
if (!client) return v4();
174-
177+
console.log("sending payload", envelope);
178+
175179
const response = await client
176180
.request<StoreMetaEnvelopeResponse>(STORE_META_ENVELOPE, {
177181
input: {
@@ -211,7 +215,7 @@ export class EVaultClient {
211215
{
212216
id,
213217
w3id,
214-
},
218+
}
215219
);
216220
return response.metaEnvelope;
217221
} catch (error) {
@@ -222,11 +226,12 @@ export class EVaultClient {
222226

223227
async updateMetaEnvelopeById(
224228
id: string,
225-
envelope: MetaEnvelope,
229+
envelope: MetaEnvelope
226230
): Promise<void> {
231+
console.log("sending to eVault", envelope.w3id);
227232
const client = await this.ensureClient(envelope.w3id).catch(() => null);
228233
if (!client) throw new Error();
229-
234+
230235
try {
231236
const variables = {
232237
id,
@@ -236,10 +241,10 @@ export class EVaultClient {
236241
acl: ["*"],
237242
},
238243
};
239-
244+
240245
const response = await client.request<StoreMetaEnvelopeResponse>(
241246
UPDATE_META_ENVELOPE,
242-
variables,
247+
variables
243248
);
244249
} catch (error) {
245250
console.error("Error updating meta envelope:", error);

0 commit comments

Comments
 (0)