Example #1
0
        public async Task <IActionResult> Register(PosRegisterInput input)
        {
            if (!User.GetUserId(out var loggedUserId))
            {
                return(Forbid());
            }

            var owningMerchant = await Mongo.GetMerchantById(input.OwnerMerchantId);

            if (owningMerchant == null)
            {
                return(Problem("Owning merchant does not exist"));
            }

            if (!owningMerchant.AdministratorIds.Contains(loggedUserId))
            {
                return(Forbid());
            }

            try {
                var posKeys = CryptoHelper.CreateKeyPair();

                var pos = new Pos {
                    MerchantId = owningMerchant.Id,
                    Name       = input.Name,
                    Position   = new GeoJsonPoint <GeoJson2DGeographicCoordinates>(
                        new GeoJson2DGeographicCoordinates(
                            input.Longitude,
                            input.Latitude
                            )
                        ),
                    Url        = input.Url,
                    PrivateKey = posKeys.Private.ToPemString(),
                    PublicKey  = posKeys.Public.ToPemString(),
                    CreatedOn  = DateTime.UtcNow,
                    IsDummy    = false
                };
                await Mongo.CreatePos(pos);

                return(CreatedAtAction(
                           nameof(GetInformation),
                           new {
                    id = pos.Id
                },
                           pos.ToOutput()
                           ));
            }
            catch (Exception ex) {
                Logger.LogError(ex, "Failed to register new POS");
                throw;
            }
        }