Create a new organization and add the authenticated user as owner.
(
*,
request: Request,
orm: Session = Depends(get_orm_session),
body: OrgCreateSchema,
)
| 224 | |
| 225 | |
| 226 | def create_org( |
| 227 | *, |
| 228 | request: Request, |
| 229 | orm: Session = Depends(get_orm_session), |
| 230 | body: OrgCreateSchema, |
| 231 | ) -> OrgResponse: |
| 232 | """ |
| 233 | Create a new organization and add the authenticated user as owner. |
| 234 | """ |
| 235 | if not (user := UserModel.get_by_id(orm, request.state.session.user_id)): |
| 236 | raise HTTPException(status_code=500, detail="User not found") |
| 237 | |
| 238 | org: OrgModel = OrgModel(name=body.name) |
| 239 | orm.add(org) |
| 240 | orm.flush() # generate the id |
| 241 | |
| 242 | # TODO user may not have an email address here |
| 243 | # this displays in the UI for the user in the list of org members |
| 244 | user_org: UserOrgModel = UserOrgModel( |
| 245 | user_id=user.id, |
| 246 | org_id=org.id, |
| 247 | role=OrgRoles.owner, |
| 248 | user_email=user.email, |
| 249 | is_paid=True, # Mark owner as paid from creation |
| 250 | ) |
| 251 | orm.add(user_org) |
| 252 | |
| 253 | orm.commit() |
| 254 | |
| 255 | # Reload with relationships to ensure we have users loaded |
| 256 | org = OrgModel.get_by_id(orm, org.id) |
| 257 | |
| 258 | org.set_current_user(request.state.session.user_id) |
| 259 | return OrgResponse.model_validate(org) |
| 260 | |
| 261 | |
| 262 | def update_org( |
searching dependent graphs…