From 8fea6116a06f3818b22444f7aca3adcf4c7de7ec Mon Sep 17 00:00:00 2001 From: John Cardinal Date: Mon, 22 Mar 2021 18:46:44 +0000 Subject: [PATCH] --- .../GlobalBizSettingsController.cs | 53 +++++++++++++------ 1 file changed, 38 insertions(+), 15 deletions(-) diff --git a/server/AyaNova/Controllers/GlobalBizSettingsController.cs b/server/AyaNova/Controllers/GlobalBizSettingsController.cs index b706771a..0e461e84 100644 --- a/server/AyaNova/Controllers/GlobalBizSettingsController.cs +++ b/server/AyaNova/Controllers/GlobalBizSettingsController.cs @@ -177,36 +177,59 @@ namespace AyaNova.Api.Controllers if (!Authorized.HasModifyRole(HttpContext.Items, AyaType.Global)) return StatusCode(403, new ApiNotAuthorizedResponse()); + + //this is random and arbitrary but is meant as a sanity check + //in v7 and int identity was used for serial numbers so the maximum possible value could be 2,147,483,647 + //assuming there is some weird edge case where people want to move beyond that max value for some reason I'm settling on a sanity check of 3 billion 3,000,000,000 + if (nextSeed == 0 || nextSeed > 3000000000) + return BadRequest(new ApiErrorResponse(ApiErrorCode.VALIDATION_INVALID_VALUE, "nextSeed", "Next seed is capped at 3,000,000,000 maximum!")); + + string SequenceName = string.Empty; + //confirm this number is not in use and set correct sequence name to alter switch (aType) { case AyaType.PurchaseOrder: + + if (await ct.PurchaseOrder.AnyAsync(z => z.Serial == nextSeed)) + return BadRequest(new ApiErrorResponse(ApiErrorCode.VALIDATION_NOT_UNIQUE, "nextSeed")); + SequenceName = "apurchaseorder_serial_seq"; break; case AyaType.WorkOrder: + if (await ct.WorkOrder.AnyAsync(z => z.Serial == nextSeed)) + return BadRequest(new ApiErrorResponse(ApiErrorCode.VALIDATION_NOT_UNIQUE, "nextSeed")); + SequenceName = "aworkorder_serial_seq"; break; case AyaType.Quote: + if (await ct.Quote.AnyAsync(z => z.Serial == nextSeed)) + return BadRequest(new ApiErrorResponse(ApiErrorCode.VALIDATION_NOT_UNIQUE, "nextSeed")); + SequenceName = "aquote_serial_seq"; break; case AyaType.PM: + if (await ct.PM.AnyAsync(z => z.Serial == nextSeed)) + return BadRequest(new ApiErrorResponse(ApiErrorCode.VALIDATION_NOT_UNIQUE, "nextSeed")); + SequenceName = "apm_serial_seq"; + break; default: return BadRequest(new ApiErrorResponse(ApiErrorCode.VALIDATION_INVALID_VALUE, "aType", "Not a serialized type")); } + try + { + using (var command = ct.Database.GetDbConnection().CreateCommand()) + { + command.CommandText = $"ALTER SEQUENCE {SequenceName} RESTART WITH {nextSeed};"; + await ct.Database.OpenConnectionAsync(); + await command.ExecuteNonQueryAsync(); + await ct.Database.CloseConnectionAsync(); + } + } + catch (Exception ex) + { + return BadRequest(new ApiErrorResponse(ApiErrorCode.API_SERVER_ERROR, "generalerror", ex.Message)); + } - - //todo: set next seed here - /* - SELECT c.relname FROM pg_class c WHERE c.relkind = 'S';" -"awidget_serial_seq" -"apurchaseorder_serial_seq" -"aworkorder_serial_seq" -"aquote_serial_seq" -"apm_serial_seq" - */ - - // var o = await biz.PutAsync(updatedObject);//In future may need to return entire object, for now just concurrency token - // if (o == null) - // return StatusCode(409, new ApiErrorResponse(biz.Errors)); - return Ok(ApiOkResponse.Response(new { Ok = 1 })); + return Ok(ApiOkResponse.Response(new { AType = aType, NextSeed = nextSeed })); }