Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
petrpavlik committed Mar 11, 2024
1 parent bb9335b commit 49ded01
Showing 1 changed file with 62 additions and 8 deletions.
70 changes: 62 additions & 8 deletions Sources/App/Controllers/ProfileController.swift
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import Fluent
import Vapor
import FirebaseJWTMiddleware

extension Request {
var profile: Profile {
get async throws {
let token = try await self.jwtUser
if let profile = try await Profile.query(on: self.db).filter(\.$firebaseUserId == token.userID).first() {

try await profile.update(on: db)

return profile

} else {
throw Abort(.notFound, reason: "Profile not found.")
}
Expand All @@ -18,20 +23,24 @@ struct ProfileDTO: Content {
var id: UUID
var email: String
var isSubscribedToNewsletter: Bool
var name: String?
var avatarUrl: String?
}

struct ProfileLiteDTO: Content {
var id: UUID
var email: String
var name: String?
var avatarUrl: String?
}

extension Profile {
func toDTO() throws -> ProfileDTO {
.init(id: try requireID(), email: email, isSubscribedToNewsletter: subscribedToNewsletterAt != nil)
return .init(id: try requireID(), email: email, isSubscribedToNewsletter: subscribedToNewsletterAt != nil, name: name, avatarUrl: avatarUrl)
}

func toLiteDTO() throws -> ProfileLiteDTO {
.init(id: try requireID(), email: email)
return .init(id: try requireID(), email: email, name: name, avatarUrl: avatarUrl)
}
}

Expand All @@ -49,7 +58,8 @@ struct ProfileController: RouteCollection {
}

func create(req: Request) async throws -> ProfileDTO {
let token = try await req.jwtUser
let token = try await req.firebaseJwt.asyncVerify()
let avatarUrl = token.picture?.replacingOccurrences(of: "\\/", with: "")
if let profile = try await Profile.query(on: req.db).filter(\.$firebaseUserId == token.userID).first() {

guard let email = token.email else {
Expand All @@ -61,15 +71,59 @@ struct ProfileController: RouteCollection {
throw Abort(.badRequest, reason: "Firebase user email does not match profile email.")
}

await req.trackAnalyticsEvent(name: "profile_created")
if profile.name != token.name {
profile.name = token.name
try await profile.update(on: req.db)
}

if profile.avatarUrl != avatarUrl {
profile.avatarUrl = avatarUrl
try await profile.update(on: req.db)
}

try await profile.update(on: req.db)


return try profile.toDTO()
} else {
guard let email = token.email else {
throw Abort(.badRequest, reason: "Firebase user does not have an email address.")
}
let profile = Profile(firebaseUserId: token.userID, email: email, name: token.name, avatarUrl: token.picture)

let profile = Profile(firebaseUserId: token.userID, email: email, name: token.name, avatarUrl: avatarUrl)
try await profile.save(on: req.db)

let invites = try await OrganizationInvite.query(on: req.db).filter(\.$email == profile.email).with(\.$organization).all()

if invites.isEmpty {
// Create default organization
let organizationName: String
if let usersName = token.name, usersName.isEmpty == false {
organizationName = "\(usersName)'s Organization"
} else {
organizationName = "Default Organization"
}

let organization = Organization(name: organizationName)
try await organization.create(on: req.db)

try await organization.$profiles.attach(profile, on: req.db) { pivot in
pivot.role = .admin
}
} else {

for invite in invites {

try await invite.organization.$profiles.attach(profile, on: req.db) { pivot in
pivot.role = invite.role
}

try await invite.delete(on: req.db)
}
}

await req.trackAnalyticsEvent(name: "profile_created")

return try profile.toDTO()
}
}
Expand All @@ -81,6 +135,7 @@ struct ProfileController: RouteCollection {
var isSubscribedToNewsletter: Bool?
}

// try ProfileUpdateDTO.validate(content: req)
let update = try req.content.decode(ProfileUpdateDTO.self)

if let isSubscribedToNewsletter = update.isSubscribedToNewsletter {
Expand All @@ -100,9 +155,8 @@ struct ProfileController: RouteCollection {

func delete(req: Request) async throws -> HTTPStatus {
// TODO: delete org if it's the last admin member
let profile = try await req.profile
try await profile.delete(on: req.db)
await req.trackAnalyticsEvent(name: "profile_deleted", params: ["email": profile.email])
try await req.profile.delete(on: req.db)
await req.trackAnalyticsEvent(name: "profile_deleted")
return .noContent
}
}

0 comments on commit 49ded01

Please sign in to comment.