plz / org_routes.v
68 lines · 63 sloc · 2.08 KB · 4de5ced2a76bc53b998b59aafbda96221ab37dfe
Raw
1// Copyright (c) 2019-2026 Alexander Medvednikov. All rights reserved.
2// Use of this source code is governed by a GPL license that can be found in the LICENSE file.
3module main
4
5import veb
6import validation
7
8@['/organizations/new']
9pub fn (mut app App) new_org(mut ctx Context) veb.Result {
10 if !ctx.logged_in {
11 return ctx.redirect_to_login()
12 }
13 return $veb.html('templates/new/org.html')
14}
15
16@['/organizations/new'; post]
17pub fn (mut app App) handle_new_org(mut ctx Context) veb.Result {
18 if !ctx.logged_in {
19 return ctx.redirect_to_login()
20 }
21 org_name := ctx.form['org_name']
22 contact_email := ctx.form['contact_email']
23 org_kind := ctx.form['org_kind']
24 accept_terms := ctx.form['accept_terms'] == '1'
25
26 if validation.is_string_empty(org_name) {
27 ctx.error('Organization name is required')
28 return app.new_org(mut ctx)
29 }
30 if org_name.len > max_username_len {
31 ctx.error('The organization name is too long (should be fewer than ${max_username_len} characters)')
32 return app.new_org(mut ctx)
33 }
34 if org_name.contains(' ') {
35 ctx.error('Organization name cannot contain spaces')
36 return app.new_org(mut ctx)
37 }
38 if validation.is_string_empty(contact_email) {
39 ctx.error('Contact email is required')
40 return app.new_org(mut ctx)
41 }
42 if org_kind != 'personal' && org_kind != 'business' {
43 ctx.error('Please select who this organization belongs to')
44 return app.new_org(mut ctx)
45 }
46 if !accept_terms {
47 ctx.error('You must accept the Terms of Service')
48 return app.new_org(mut ctx)
49 }
50 if _ := app.get_user_by_username(org_name) {
51 ctx.error('The name "${org_name}" is already taken')
52 return app.new_org(mut ctx)
53 }
54 if _ := app.get_org_by_name(org_name) {
55 ctx.error('The name "${org_name}" is already taken')
56 return app.new_org(mut ctx)
57 }
58
59 org_id := app.add_org(org_name, contact_email, org_kind, ctx.user.id) or {
60 ctx.error('Could not create organization: ${err}')
61 return app.new_org(mut ctx)
62 }
63 app.add_org_member(org_id, ctx.user.id, 'admin') or {
64 ctx.error('Could not add you as the organization owner: ${err}')
65 return app.new_org(mut ctx)
66 }
67 return ctx.redirect('/new?owner=${org_name}')
68}
69