Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions platforms/eCurrency-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"cors": "^2.8.5",
"dotenv": "^16.4.5",
"express": "^4.18.2",
"graphql-request": "^6.1.0",
"jsonwebtoken": "^9.0.2",
"pg": "^8.11.3",
"reflect-metadata": "^0.2.1",
Expand Down
85 changes: 85 additions & 0 deletions platforms/eCurrency-api/src/controllers/WebhookController.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
import { Request, Response } from "express";
import { UserService } from "../services/UserService";
import { GroupService } from "../services/GroupService";
import { MessageService } from "../services/MessageService";
import { adapter } from "../web3adapter/watchers/subscriber";
import { User } from "../database/entities/User";
import { Group } from "../database/entities/Group";
import { Message } from "../database/entities/Message";
import axios from "axios";

export class WebhookController {
userService: UserService;
groupService: GroupService;
messageService: MessageService;
adapter: typeof adapter;

constructor() {
this.userService = new UserService();
this.groupService = new GroupService();
this.messageService = new MessageService();
this.adapter = adapter;
}

Expand Down Expand Up @@ -179,6 +184,86 @@ export class WebhookController {
finalLocalId = group.id;
}
}
} else if (mapping.tableName === "messages") {
console.log("Processing message with data:", local.data);

// Extract sender and group from the message data
let sender: User | null = null;
let group: Group | null = null;

if (local.data.sender && typeof local.data.sender === "string") {
const senderId = local.data.sender.split("(")[1].split(")")[0];
sender = await this.userService.getUserById(senderId);
}

if (local.data.group && typeof local.data.group === "string") {
const groupId = local.data.group.split("(")[1].split(")")[0];
group = await this.groupService.getGroupById(groupId);
}

// Check if this is a system message (no sender required)
const isSystemMessage = local.data.isSystemMessage === true ||
(local.data.text && typeof local.data.text === 'string' && local.data.text.startsWith('$$system-message$$'));

if (!group) {
console.error("Group not found for message");
return res.status(500).send();
}

// For system messages, sender can be null
if (!isSystemMessage && !sender) {
console.error("Sender not found for non-system message");
return res.status(500).send();
}

if (localId) {
console.log("Updating existing message with localId:", localId);
const message = await this.messageService.getMessageById(localId);
if (!message) {
console.error("Message not found for localId:", localId);
return res.status(500).send();
}

// For system messages, ensure the prefix is preserved
if (isSystemMessage && !(local.data.text as string).startsWith('$$system-message$$')) {
message.text = `$$system-message$$ ${local.data.text as string}`;
} else {
message.text = local.data.text as string;
}
message.sender = sender || undefined;
message.group = group;
message.isSystemMessage = isSystemMessage as boolean;
Comment on lines +227 to +235
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Potential data loss: sender field may be unintentionally cleared.

On line 233, setting message.sender = sender || undefined will set sender to undefined if the sender lookup fails, even if the message previously had a valid sender. This could unintentionally clear the sender field during updates.

Consider preserving the existing sender if lookup fails:

                     if (isSystemMessage && !(local.data.text as string).startsWith('$$system-message$$')) {
                         message.text = `$$system-message$$ ${local.data.text as string}`;
                     } else {
                         message.text = local.data.text as string;
                     }
-                    message.sender = sender || undefined;
+                    // Only update sender if explicitly provided (preserve existing sender if lookup fails)
+                    if (local.data.sender) {
+                        message.sender = sender || undefined;
+                    }
                     message.group = group;
                     message.isSystemMessage = isSystemMessage as boolean;
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// For system messages, ensure the prefix is preserved
if (isSystemMessage && !(local.data.text as string).startsWith('$$system-message$$')) {
message.text = `$$system-message$$ ${local.data.text as string}`;
} else {
message.text = local.data.text as string;
}
message.sender = sender || undefined;
message.group = group;
message.isSystemMessage = isSystemMessage as boolean;
// For system messages, ensure the prefix is preserved
if (isSystemMessage && !(local.data.text as string).startsWith('$$system-message$$')) {
message.text = `$$system-message$$ ${local.data.text as string}`;
} else {
message.text = local.data.text as string;
}
// Only update sender if explicitly provided (preserve existing sender if lookup fails)
if (local.data.sender) {
message.sender = sender || undefined;
}
message.group = group;
message.isSystemMessage = isSystemMessage as boolean;
🤖 Prompt for AI Agents
In platforms/eCurrency-api/src/controllers/WebhookController.ts around lines 227
to 235, the code currently assigns message.sender = sender || undefined which
will clear an existing sender when the lookup returns a falsy value; change the
logic to only overwrite message.sender when sender is a valid value (e.g.,
non-null/defined) so that if the lookup fails the existing message.sender is
preserved, and ensure types are respected (cast or guard as needed) when
conditionally assigning.


this.adapter.addToLockedIds(localId);
await this.messageService.messageRepository.save(message);
console.log("Updated message:", message.id);
finalLocalId = message.id;
} else {
console.log("Creating new message");
let message: Message;

if (isSystemMessage) {
message = await this.messageService.createSystemMessageWithoutPrefix({
text: local.data.text as string,
groupId: group.id,
});
} else {
message = await this.messageService.createMessage({
text: local.data.text as string,
senderId: sender!.id, // We know sender exists for non-system messages
groupId: group.id,
});
}

console.log("Created message with ID:", message.id);
this.adapter.addToLockedIds(message.id);
await this.adapter.mappingDb.storeMapping({
localId: message.id,
globalId: req.body.id,
});
console.log("Stored mapping for message:", message.id, "->", req.body.id);
finalLocalId = message.id;
}
}

res.status(200).send();
Expand Down
4 changes: 3 additions & 1 deletion platforms/eCurrency-api/src/database/data-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { User } from "./entities/User";
import { Group } from "./entities/Group";
import { Currency } from "./entities/Currency";
import { Ledger } from "./entities/Ledger";
import { Message } from "./entities/Message";
import { UserEVaultMapping } from "./entities/UserEVaultMapping";
import { PostgresSubscriber } from "../web3adapter/watchers/subscriber";

// Use absolute path for better CLI compatibility
Expand All @@ -16,7 +18,7 @@ export const dataSourceOptions: DataSourceOptions = {
type: "postgres",
url: process.env.ECURRENCY_DATABASE_URL,
synchronize: false, // Auto-sync in development
entities: [User, Group, Currency, Ledger],
entities: [User, Group, Currency, Ledger, Message, UserEVaultMapping],
migrations: [path.join(__dirname, "migrations", "*.ts")],
logging: process.env.NODE_ENV === "development",
subscribers: [PostgresSubscriber],
Expand Down
5 changes: 5 additions & 0 deletions platforms/eCurrency-api/src/database/entities/Group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import {
PrimaryGeneratedColumn,
Column,
ManyToMany,
OneToMany,
JoinTable,
} from "typeorm";
import { User } from "./User";
import { Message } from "./Message";

@Entity()
export class Group {
Expand Down Expand Up @@ -68,6 +70,9 @@ export class Group {
@Column({ type: "json", nullable: true })
originalMatchParticipants!: string[]; // Store user IDs from the original match

@OneToMany(() => Message, (message) => message.group)
messages!: Message[];

@CreateDateColumn()
createdAt!: Date;

Expand Down
41 changes: 41 additions & 0 deletions platforms/eCurrency-api/src/database/entities/Message.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import {
Entity,
PrimaryGeneratedColumn,
Column,
CreateDateColumn,
UpdateDateColumn,
ManyToOne,
} from "typeorm";
import { User } from "./User";
import { Group } from "./Group";

@Entity("messages")
export class Message {
@PrimaryGeneratedColumn("uuid")
id!: string;

@ManyToOne(() => User, { nullable: true })
sender?: User; // Nullable for system messages

@Column("text")
text!: string;

@ManyToOne(() => Group, (group) => group.messages)
group!: Group;

@Column({ default: false })
isSystemMessage!: boolean; // Flag to identify system messages

@Column("uuid", { nullable: true })
voteId?: string; // ID of the vote/poll this system message relates to

@CreateDateColumn()
createdAt!: Date;

@UpdateDateColumn()
updatedAt!: Date;

@Column({ default: false })
isArchived!: boolean;
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import {
Entity,
PrimaryGeneratedColumn,
Column,
CreateDateColumn,
UpdateDateColumn,
} from "typeorm";

@Entity("user_evault_mappings")
export class UserEVaultMapping {
@PrimaryGeneratedColumn("uuid")
id!: string;

@Column()
localUserId!: string;

@Column()
evaultW3id!: string;

@Column()
evaultUri!: string;

@Column({ nullable: true })
userProfileId!: string; // ID of the UserProfile object in the eVault

@Column({ type: "jsonb", nullable: true })
userProfileData!: any; // Store the UserProfile data

@CreateDateColumn()
createdAt!: Date;

@UpdateDateColumn()
updatedAt!: Date;
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { MigrationInterface, QueryRunner } from "typeorm";

export class Migration1765208128946 implements MigrationInterface {
name = 'Migration1765208128946'

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`CREATE TABLE "messages" ("id" uuid NOT NULL DEFAULT uuid_generate_v4(), "text" text NOT NULL, "isSystemMessage" boolean NOT NULL DEFAULT false, "voteId" uuid, "createdAt" TIMESTAMP NOT NULL DEFAULT now(), "updatedAt" TIMESTAMP NOT NULL DEFAULT now(), "isArchived" boolean NOT NULL DEFAULT false, "senderId" uuid, "groupId" uuid, CONSTRAINT "PK_18325f38ae6de43878487eff986" PRIMARY KEY ("id"))`);
await queryRunner.query(`CREATE TABLE "user_evault_mappings" ("id" uuid NOT NULL DEFAULT uuid_generate_v4(), "localUserId" character varying NOT NULL, "evaultW3id" character varying NOT NULL, "evaultUri" character varying NOT NULL, "userProfileId" character varying, "userProfileData" jsonb, "createdAt" TIMESTAMP NOT NULL DEFAULT now(), "updatedAt" TIMESTAMP NOT NULL DEFAULT now(), CONSTRAINT "PK_744ddb4ddca6af2de54773e9213" PRIMARY KEY ("id"))`);
await queryRunner.query(`ALTER TABLE "messages" ADD CONSTRAINT "FK_2db9cf2b3ca111742793f6c37ce" FOREIGN KEY ("senderId") REFERENCES "users"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`);
await queryRunner.query(`ALTER TABLE "messages" ADD CONSTRAINT "FK_438f09ab5b4bbcd27683eac2a5e" FOREIGN KEY ("groupId") REFERENCES "group"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "messages" DROP CONSTRAINT "FK_438f09ab5b4bbcd27683eac2a5e"`);
await queryRunner.query(`ALTER TABLE "messages" DROP CONSTRAINT "FK_2db9cf2b3ca111742793f6c37ce"`);
await queryRunner.query(`DROP TABLE "user_evault_mappings"`);
await queryRunner.query(`DROP TABLE "messages"`);
}

}
18 changes: 18 additions & 0 deletions platforms/eCurrency-api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { CurrencyController } from "./controllers/CurrencyController";
import { LedgerController } from "./controllers/LedgerController";
import { authMiddleware, authGuard } from "./middleware/auth";
import { adapter } from "./web3adapter/watchers/subscriber";
import { PlatformEVaultService } from "./services/PlatformEVaultService";

config({ path: path.resolve(__dirname, "../../../.env") });

Expand All @@ -24,6 +25,23 @@ AppDataSource.initialize()
.then(async () => {
console.log("Database connection established");
console.log("Web3 adapter initialized");

// Initialize platform eVault for eCurrency
try {
const platformService = PlatformEVaultService.getInstance();
const exists = await platformService.checkPlatformEVaultExists();

if (!exists) {
console.log("🔧 Creating platform eVault for eCurrency...");
const result = await platformService.createPlatformEVault();
console.log(`✅ Platform eVault created successfully: ${result.w3id}`);
} else {
console.log("✅ Platform eVault already exists for eCurrency");
}
} catch (error) {
console.error("❌ Failed to initialize platform eVault:", error);
// Don't exit the process, just log the error
}
})
.catch((error: unknown) => {
console.error("Error during initialization:", error);
Expand Down
Loading