Skip to content
Open
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
25 changes: 25 additions & 0 deletions app/createUser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Logger } from "@aws-lambda-powertools/logger";
import { UserManager } from "./service/user-manager";
import { CustomError } from "./utils/feedback-util";
import { UserModel } from "./model/user-model";



const logger = new Logger({
logLevel:"DEBUG",
serviceName:"createUser"
})

const userManager = new UserManager()

export async function handler(event:any) {
logger.info(`ℹ️ Event received: ${JSON.stringify(event)}`)
try {
const {user} = event.argumments
let userModel = new UserModel(user.id || '',user.name,user.email,user.password)
return await userManager.creatUser(userModel)
} catch (error:any) {
logger.error(`❌ Error handling event: ${error.message}`)
throw new CustomError(500, `❌ Error handling event: ${error.message}`)
}
}
59 changes: 59 additions & 0 deletions app/model/user-model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { randomUUID } from "crypto";
import { User } from "../types/types";

class UserModel {
public id: string;
public name: string;
public email: string;
public password: string;
public entity: string

constructor(
id?: string,
name?: string,
email?: string,
password?: string
) {
this.id = id || randomUUID();
this.name = name || "";
this.email = email || '';
this.password = password || '';
this.entity = "USER"
}

get pk() {
return `${this.entity}#${this.id}`;
}

get sk() {
return `${this.entity}#${this.id}`;
}

get data(): User {
return {
id: this.id,
name: this.name,
email: this.email,
password: this.password
}
}

toItem(): Record<string, unknown> {
return {
PK: this.pk,
SK: this.sk,
data: this.data,
entity: this.entity
};
}

static fromItem(item: any) {
if (!item || !item.data) {
throw new Error(`❌ Invalid item received: ${item}`);
}
const {name, id, email, password } = item.data as User;
return new UserModel(id, name, email, password);
}

}
export {UserModel}
Loading