Skip to content

Commit 919a223

Browse files
committed
Switched to update
For some reason, if we try to run .save() after removing the last domain in a users list, the empty array is not saved and the last domain is not deleted. Domains can be deleted if there are more than one, however the very last domain could not. A similar issue was seen when adding an alias, hence that has also been changed to .update() The return types of the two methods have been changed, however they are only used in one place each and the return values are not used so there is no issue here. Signed-off-by: Matthew Nickson <mnickson@sidingsmedia.com>
1 parent 4f61406 commit 919a223

1 file changed

Lines changed: 5 additions & 5 deletions

File tree

src/users/users.service.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { Package } from 'src/packages/package.entity'
1818
import { PackagesService } from 'src/packages/packages.service'
1919
import { DeleteForDomainData } from 'src/tasks/delete-for-domain/delete-for-domain.interfaces'
2020
import { SuspensionData } from 'src/tasks/suspension/suspension.interfaces'
21-
import { MongoRepository } from 'typeorm'
21+
import { MongoRepository, UpdateResult } from 'typeorm'
2222

2323
import { CreateUserDto } from './dto/create-user.dto'
2424
import { UpdateUserDto } from './dto/update-user.dto'
@@ -137,7 +137,7 @@ export class UsersService {
137137
}
138138
}
139139

140-
public async pullDomain(userId: string, domain: string): Promise<User> {
140+
public async pullDomain(userId: string, domain: string): Promise<UpdateResult> {
141141
const user = await this.findByIdNoPassword(userId)
142142
const userEntity = new User()
143143
Object.assign(userEntity, user)
@@ -146,7 +146,7 @@ export class UsersService {
146146
userEntity.domains = userEntity.domains.filter((domainObject) => domainObject.domain !== domain)
147147

148148
try {
149-
return this.userRepository.save(userEntity)
149+
return this.userRepository.update({ _id: userEntity._id }, { domains: userEntity.domains })
150150
} catch (error) {
151151
// TODO: add custom exception handler for unknown errors that basically does the following:
152152
const errorId = NanoId()
@@ -155,7 +155,7 @@ export class UsersService {
155155
}
156156
}
157157

158-
public async pushAlias(userId: string, domain: string, alias: DomainAlias): Promise<User | undefined> {
158+
public async pushAlias(userId: string, domain: string, alias: DomainAlias): Promise<UpdateResult | undefined> {
159159
const user = await this.findByIdNoPassword(userId)
160160
const userEntity = new User()
161161
Object.assign(userEntity, user)
@@ -171,7 +171,7 @@ export class UsersService {
171171
})
172172

173173
try {
174-
return await this.userRepository.save(userEntity)
174+
return await this.userRepository.update({ _id: userEntity._id }, { domains: userEntity.domains })
175175
} catch (error) {
176176
const errorId = NanoId()
177177
this.logger.error(`${errorId}: ${error.message}`)

0 commit comments

Comments
 (0)