public async Task <IActionResult> Invite(int companyId, string responseUrl, [FromBody] CompanyInvitationRequest request)
        {
            if (await db.CompanyInvitations.AnyAsync(x => x.CompanyId == companyId && x.Email == request.Email))
            {
                return(this.BadRequest(nameof(CompanyInvitation.Email), "An invitation has already been sent to this email."));
            }
            var invitation = new CompanyInvitation();

            invitation.Token       = Guid.NewGuid();
            invitation.CompanyId   = companyId;
            invitation.Email       = request.Email;
            invitation.Status      = InvitationStatus.Pending;
            invitation.Permissions = string.Join(",", request.Permissions);
            db.Add(invitation);
            await db.SaveChangesAsync();

            var companyName = await db.Companies
                              .Where(x => x.CompanyId == companyId)
                              .Select(x => x.Name)
                              .SingleAsync();

            await emailingClient.SendAsync(new EmailRequest
            {
                Data = new Dictionary <string, string>
                {
                    ["CompanyName"] = companyName,
                    ["ResponseUrl"] = string.Format(WebUtility.UrlDecode(responseUrl), invitation.Token)
                },
                Subject     = "Company Invitation",
                TemplateKey = "CompanyInvitation",
                To          = request.Email
            });

            var responseUri = new Uri(string.Format(WebUtility.UrlDecode(responseUrl), invitation.Token));
            var user        = await usersClient.GetUserByEmailAsync(request.Email);

            if (user != null)
            {
                await notificationsClient.NotifyAsync(user.UserId, new NotifyRequest
                {
                    RedirectPath = responseUri.PathAndQuery,
                    Subject      = $"Company Invitation",
                    Body         = $"You have been invited to to join the company <strong>{companyName}</strong>."
                });
            }

            return(NoContent());
        }
Beispiel #2
0
        public async Task <Unit> Handle(LogApiRequestCommand request, CancellationToken cancellationToken)
        {
            dbContext.Add(new ApiRequest()
            {
                Id          = request.Id,
                Timestamp   = request.Timespan,
                RequestData = JsonSerializer.Serialize(request.RequestData)
            });

            await dbContext.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
        public async Task <IActionResult> Create([FromBody] SaveCompanyRequest request)
        {
            if (await db.Companies.AnyAsync(x => x.UniqueName == request.UniqueName))
            {
                return(this.BadRequest(nameof(SaveCompanyRequest.UniqueName), UniqueNameNotUnique));
            }
            var company = new Company
            {
                Name            = request.Name,
                UniqueName      = request.UniqueName,
                CreatedAt       = DateTime.Now,
                Status          = CompanyStatus.Active,
                Logo            = request.Logo,
                LogoContentType = request.LogoContentType
            };

            db.Add(company);
            var userId      = this.GetUserId();
            var companyUser = new CompanyUser
            {
                Company    = company,
                UserId     = userId,
                IsFavorite = true
            };

            db.Add(companyUser);
            foreach (var type in (CompanyUserPermissionType[])Enum.GetValues(typeof(CompanyUserPermissionType)))
            {
                db.CompanyUserPermissions.Add(new CompanyUserPermission
                {
                    Type        = type,
                    CompanyUser = companyUser,
                    IsSet       = true,
                });
            }
            await db.SaveChangesAsync();

            return(Ok(company.CompanyId));
        }