Skip to content

Commit 00c1150

Browse files
authored
Mass convert to slots: skipping project on error and add logging (#2538)
1 parent d2759f8 commit 00c1150

File tree

2 files changed

+93
-78
lines changed

2 files changed

+93
-78
lines changed

src/JoinRpg.Portal/Pages/Admin/ConvertInactiveProjectsToSlots.cshtml.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,17 @@ public async Task OnGet()
2121

2222
foreach (var projectId in projects)
2323
{
24-
var id = new PrimitiveTypes.ProjectIdentification(projectId);
25-
logger.LogInformation("About to convert {projectId} to slots", id);
26-
await slotMassConvertService.MassConvert(id);
27-
logger.LogInformation("Converted {projectId} to slots", id);
24+
try
25+
{
26+
var id = new PrimitiveTypes.ProjectIdentification(projectId);
27+
logger.LogInformation("About to convert {projectId} to slots", id);
28+
await slotMassConvertService.MassConvert(id);
29+
logger.LogInformation("Converted {projectId} to slots", id);
30+
}
31+
catch (Exception exception)
32+
{
33+
logger.LogError(exception, "Failing to convert {projectId} to slots, skipping", projectId);
34+
}
2835
}
2936

3037
}

src/JoinRpg.Services.Impl/CharacterServiceImpl.cs

Lines changed: 82 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -219,100 +219,108 @@ public async Task SetFields(int projectId, int characterId, Dictionary<int, stri
219219

220220
public async Task<int?> CreateSlotFromGroup(int projectId, int characterGroupId, string slotName, bool allowToChangeInactive)
221221
{
222-
var group = await LoadProjectSubEntityAsync<CharacterGroup>(projectId, characterGroupId);
223-
224-
var projectInfo = await projectMetadataRepository.GetProjectMetadata(new(projectId));
225-
226-
if (!allowToChangeInactive)
222+
try
227223
{
228-
group.Project.EnsureProjectActive();
229-
}
230-
231-
if (!IsCurrentUserAdmin)
232-
{
233-
group.Project
234-
.RequestMasterAccess(CurrentUserId, acl => acl.CanEditRoles);
235-
}
224+
var group = await LoadProjectSubEntityAsync<CharacterGroup>(projectId, characterGroupId);
236225

237-
var claims = group.Claims.ToList();
226+
var projectInfo = await projectMetadataRepository.GetProjectMetadata(new(projectId));
238227

239-
var needToSaveClaims = claims.Any();
240-
var needToInitSlot = group.HaveDirectSlots && group.Project.Active;
241-
var needToClearSlot = group.HaveDirectSlots;
242-
243-
logger.LogInformation("Group (Id={characterGroupId}, Name={characterGroupName}) is evaluated to convert to slot. Decision (SaveClaims: {needToSaveClaims}, InitSlot: {needToInitSlot}, ClearSlot: {needToClearSlot})",
244-
characterGroupId,
245-
group.CharacterGroupName,
246-
needToSaveClaims,
247-
needToInitSlot,
248-
needToClearSlot);
228+
if (!allowToChangeInactive)
229+
{
230+
group.Project.EnsureProjectActive();
231+
}
249232

250-
if (!needToSaveClaims && !needToInitSlot && !needToClearSlot)
251-
{
252-
return null; // Do nothing
253-
}
233+
if (!IsCurrentUserAdmin)
234+
{
235+
group.Project
236+
.RequestMasterAccess(CurrentUserId, acl => acl.CanEditRoles);
237+
}
254238

255-
MarkTreeModified(group.Project);
239+
var claims = group.Claims.ToList();
256240

257-
Character? character;
258-
if (needToSaveClaims || needToInitSlot)
259-
{
241+
var needToSaveClaims = claims.Any();
242+
var needToInitSlot = group.HaveDirectSlots && group.Project.Active;
243+
var needToClearSlot = group.HaveDirectSlots;
260244

261-
var addCharacterRequest = new AddCharacterRequest(
262-
projectId,
263-
new[] { characterGroupId },
264-
CharacterTypeInfo.DefaultSlot(slotName),
265-
new Dictionary<int, string?>());
245+
logger.LogInformation("Group (Id={characterGroupId}, Name={characterGroupName}) is evaluated to convert to slot. Decision (SaveClaims: {needToSaveClaims}, InitSlot: {needToInitSlot}, ClearSlot: {needToClearSlot})",
246+
characterGroupId,
247+
group.CharacterGroupName,
248+
needToSaveClaims,
249+
needToInitSlot,
250+
needToClearSlot);
266251

267-
character = new Character
252+
if (!needToSaveClaims && !needToInitSlot && !needToClearSlot)
268253
{
269-
ParentCharacterGroupIds =
270-
await ValidateCharacterGroupList(addCharacterRequest.ProjectId, Required(addCharacterRequest.ParentCharacterGroupIds)),
271-
ProjectId = addCharacterRequest.ProjectId,
272-
Project = group.Project,
273-
};
274-
275-
SetCharacterSettings(character, addCharacterRequest.CharacterTypeInfo, projectInfo);
276-
277-
Create(character);
278-
254+
return null; // Do nothing
255+
}
279256

280-
//TODO we do not send message for creating character
281-
_ = fieldSaveHelper.SaveCharacterFields(CurrentUserId,
282-
character,
283-
addCharacterRequest.FieldValues, projectInfo);
257+
MarkTreeModified(group.Project);
284258

285-
if (needToInitSlot)
259+
Character? character;
260+
if (needToSaveClaims || needToInitSlot)
286261
{
287-
// Move limit to character
288-
character.CharacterSlotLimit = group.DirectSlotsUnlimited ? null : group.AvaiableDirectSlots;
289-
character.IsActive = true;
262+
263+
var addCharacterRequest = new AddCharacterRequest(
264+
projectId,
265+
new[] { characterGroupId },
266+
CharacterTypeInfo.DefaultSlot(slotName),
267+
new Dictionary<int, string?>());
268+
269+
character = new Character
270+
{
271+
ParentCharacterGroupIds =
272+
await ValidateCharacterGroupList(addCharacterRequest.ProjectId, Required(addCharacterRequest.ParentCharacterGroupIds)),
273+
ProjectId = addCharacterRequest.ProjectId,
274+
Project = group.Project,
275+
};
276+
277+
SetCharacterSettings(character, addCharacterRequest.CharacterTypeInfo, projectInfo);
278+
279+
Create(character);
280+
281+
282+
//TODO we do not send message for creating character
283+
_ = fieldSaveHelper.SaveCharacterFields(CurrentUserId,
284+
character,
285+
addCharacterRequest.FieldValues, projectInfo);
286+
287+
if (needToInitSlot)
288+
{
289+
// Move limit to character
290+
character.CharacterSlotLimit = group.DirectSlotsUnlimited ? null : group.AvaiableDirectSlots;
291+
character.IsActive = true;
292+
}
293+
else
294+
{
295+
character.CharacterSlotLimit = 0;
296+
character.IsActive = claims.Any(c => c.IsPending); // if there is some alive claim
297+
}
298+
299+
// Move claims from group to character
300+
301+
foreach (var claim in claims)
302+
{
303+
claim.CharacterGroupId = null;
304+
claim.Character = character;
305+
}
290306
}
291307
else
292308
{
293-
character.CharacterSlotLimit = 0;
294-
character.IsActive = claims.Any(c => c.IsPending); // if there is some alive claim
309+
character = null;
295310
}
296311

297-
// Move claims from group to character
312+
//Remove direct claim settings for group
313+
group.HaveDirectSlots = false;
314+
group.AvaiableDirectSlots = 0;
298315

299-
foreach (var claim in claims)
300-
{
301-
claim.CharacterGroupId = null;
302-
claim.Character = character;
303-
}
316+
await UnitOfWork.SaveChangesAsync();
317+
318+
return character?.CharacterId;
304319
}
305-
else
320+
catch (Exception exception)
306321
{
307-
character = null;
322+
logger.LogError(exception, "Error during converting CharacterGroup={characterGroupId}", characterGroupId);
323+
throw;
308324
}
309-
310-
//Remove direct claim settings for group
311-
group.HaveDirectSlots = false;
312-
group.AvaiableDirectSlots = 0;
313-
314-
await UnitOfWork.SaveChangesAsync();
315-
316-
return character?.CharacterId;
317325
}
318326
}

0 commit comments

Comments
 (0)