deleteProfileForUser static method
Future<void>
deleteProfileForUser(
- Session session,
- UuidValue authUserId, {
- Transaction? transaction,
Remove the user profile, incl. images, for the given authUserId
.
In case the user did not have a profile, nothing is changed.
Returns successfully if the profile data was removed from the database, even if some profile images could not be deleted from the file storage.
In case the transaction
is later rolled back, the deleted images will
still be missing from the storage.
Implementation
static Future<void> deleteProfileForUser(
final Session session,
final UuidValue authUserId, {
final Transaction? transaction,
}) async {
return DatabaseUtil.runInTransactionOrSavepoint(
session.db,
transaction,
(final transaction) async {
final profile = await UserProfile.db.findFirstRow(
session,
where: (final t) => t.authUserId.equals(authUserId),
transaction: transaction,
);
if (profile == null) {
return;
}
final images = await UserProfileImage.db.find(
session,
where: (final t) => t.userProfileId.equals(profile.id!),
transaction: transaction,
);
// This also deletes the user profile image entries from the database
await UserProfile.db.deleteWhere(
session,
where: (final t) => t.authUserId.equals(authUserId),
transaction: transaction,
);
for (final image in images) {
try {
await session.storage.deleteFile(
storageId: image.storageId,
path: image.path,
);
} catch (e, stackTrace) {
session.log(
'Failed to delete user image from storage',
level: LogLevel.error,
exception: e,
stackTrace: stackTrace,
);
}
}
},
);
}