case 4221

This commit is contained in:
2022-10-26 21:49:22 +00:00
parent 0a80ceaf1c
commit 3806cca559
2 changed files with 66 additions and 21 deletions

View File

@@ -233,13 +233,14 @@ namespace AyaNova.Api.Controllers
/// <summary>
/// Permanently erase all data and all attachments
/// Permanently erase most data and all attachments for seeding or migration purposes
///
/// Posting to this route causes AyaNova completely erase all data in it's database and erase all attachment files
/// returning the database to an empty state
/// This route is used by the v8-migration utility and by the AyaNova UI when seeding a trial database
///
/// The only items retained are the SuperUser account and the license key
///
/// Items retained are documented here with the exception of Tax codes:
/// https://ayanova.com/docs/adm-license/#erase-database
/// However not Tax Codes as documented because seeding or migration will create tax codes
///
/// (Only *the* SuperUser account can use this route)
/// </summary>
/// <param name="acceptCode">Must be "I understand"</param>
@@ -265,7 +266,51 @@ namespace AyaNova.Api.Controllers
//empty the db
await AyaNova.Util.DbUtil.EmptyBizDataFromDatabaseForSeedingOrImportingAsync(log, Guid.Empty, serverState.IsMigrateMode);
await AyaNova.Util.DbUtil.EmptyBizDataFromDatabaseForSeedingOrImportingAsync(log);
//Log
await EventLogProcessor.LogEventToDatabaseAsync(new Event(1, 0, AyaType.Global, AyaEvent.EraseAllData), ct);
return NoContent();
}
/// <summary>
/// Permanently erase most data and all attachments
///
/// This route is used by the by the AyaNova UI when a user selects to manually erase the database
/// or when they are evaluating and request another trial period with a database that already has a trial license in it
///
/// Items retained are documented here:
/// https://ayanova.com/docs/adm-license/#erase-database
///
///
/// (Only *the* SuperUser account can use this route)
/// </summary>
/// <param name="acceptCode">Must be "I understand"</param>
/// <returns>HTTP 204 No Content result code on success or fail code with explanation</returns>
[HttpPost("permanently-erase-all-data-keep-tax-codes")]
public async Task<IActionResult> RemoveAllDataKeepTaxCodes([FromBody] string acceptCode)
{
if (serverState.IsClosed)
{
//Exception for SuperUser account to handle licensing issues
if (UserIdFromContext.Id(HttpContext.Items) != 1)
return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
}
if (!ModelState.IsValid)
return BadRequest(new ApiErrorResponse(ModelState));
long UserId = UserIdFromContext.Id(HttpContext.Items);
//SuperUser only and must have accept code
if (UserId != 1 || string.IsNullOrWhiteSpace(acceptCode) || acceptCode.ToLowerInvariant() != "i understand")
return StatusCode(403, new ApiNotAuthorizedResponse());
//empty the db
await AyaNova.Util.DbUtil.EmptyBizDataFromDatabaseForSeedingOrImportingAsync(log, true);
//Log
await EventLogProcessor.LogEventToDatabaseAsync(new Event(1, 0, AyaType.Global, AyaEvent.EraseAllData), ct);
@@ -292,7 +337,7 @@ namespace AyaNova.Api.Controllers
return StatusCode(403, new ApiNotAuthorizedResponse());
await Core.License.FlagEULA(ct, log);
//Log
await EventLogProcessor.LogEventToDatabaseAsync(new Event(1, 0, AyaType.Global, AyaEvent.Modified, "End user license agreement consent obtained"), ct);

View File

@@ -395,7 +395,7 @@ namespace AyaNova.Util
// Erase all user entered data from the db
// This is called by seeder for trial seeding purposes
// and by v8 migrate and by license controller when erasing db
internal static async Task EmptyBizDataFromDatabaseForSeedingOrImportingAsync(ILogger _log, Guid jobIdToKeep, bool isMigrate = false)
internal static async Task EmptyBizDataFromDatabaseForSeedingOrImportingAsync(ILogger _log, bool keepTaxCodes=false)
{
//case 4221
//erase plan to use truncate table instead of slower delete method
@@ -408,7 +408,7 @@ namespace AyaNova.Util
bool forSeeding = jobIdToKeep != Guid.Empty;
_log.LogInformation("Erasing Database \"{0}\"", _dbName);
AyaNova.Api.ControllerHelpers.ApiServerState apiServerState = (AyaNova.Api.ControllerHelpers.ApiServerState)ServiceProviderProvider.Provider.GetService(typeof(AyaNova.Api.ControllerHelpers.ApiServerState));
@@ -455,18 +455,18 @@ namespace AyaNova.Util
cmd.CommandText = "update aworkorderitem set fromcsrid=null;";
await cmd.ExecuteNonQueryAsync();
if (jobIdToKeep != Guid.Empty)
{
//delete from aopsjoblog where jobid <> '8acec231-aab4-4fdc-b01e-8908378a0e7f';
//delete from aopsjob where gid <> '8acec231-aab4-4fdc-b01e-8908378a0e7f';
cmd.CommandText = $"delete from aopsjoblog where jobid <> '{jobIdToKeep}'";
await cmd.ExecuteNonQueryAsync();
cmd.CommandText = $"delete from aopsjob where gid <> '{jobIdToKeep}'";
await cmd.ExecuteNonQueryAsync();
}
// if (jobIdToKeep != Guid.Empty)
// {
// //delete from aopsjoblog where jobid <> '8acec231-aab4-4fdc-b01e-8908378a0e7f';
// //delete from aopsjob where gid <> '8acec231-aab4-4fdc-b01e-8908378a0e7f';
// cmd.CommandText = $"delete from aopsjoblog where jobid <> '{jobIdToKeep}'";
// await cmd.ExecuteNonQueryAsync();
// cmd.CommandText = $"delete from aopsjob where gid <> '{jobIdToKeep}'";
// await cmd.ExecuteNonQueryAsync();
// }
if (forSeeding || isMigrate)
if (!keepTaxCodes)
{
cmd.CommandText = "update aglobalbizsettings set taxpartpurchaseid=null,taxpartsaleid=null,taxratesaleid=null;";
await cmd.ExecuteNonQueryAsync();
@@ -482,7 +482,7 @@ namespace AyaNova.Util
//TAX CODES are an edge case where they are generated by the seeder potentially so need to be erased in that case or migrate but might be kept when manually erasing from UI.
//await EraseTableAsync("aopsjoblog", conn, true);
// await EraseTableAsync("aopsjob", conn, true);
@@ -613,7 +613,7 @@ namespace AyaNova.Util
await EraseTableAsync("aservicerate", conn);
await EraseTableAsync("atravelrate", conn);
if (forSeeding || isMigrate)
if (!keepTaxCodes)
await EraseTableAsync("ataxcode", conn);