Skip to content

Commit

Permalink
Implement deleteAccount Sync endpoint (#596)
Browse files Browse the repository at this point in the history
Task/Issue URL: https://app.asana.com/0/1201493110486074/1206055891122348/f

Description:
Add missing delete-account endpoint and update implementation to call it instead of logging out all devices.
  • Loading branch information
ayoy authored and samsymons committed Dec 7, 2023
1 parent cd89124 commit 450f2b4
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 12 deletions.
15 changes: 5 additions & 10 deletions Sources/DDGSync/internal/AccountManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -143,17 +143,12 @@ struct AccountManager: AccountManaging {
throw SyncError.noToken
}

let devices = try await fetchDevicesForAccount(account)

// Logout other devices first or the call will fail
for device in devices.filter({ $0.id != account.deviceId }) {
try await logout(deviceId: device.id, token: token)
}
let request = api.createAuthenticatedJSONRequest(url: endpoints.deleteAccount, method: .POST, authToken: token)
let result = try await request.execute()
let statusCode = result.response.statusCode

// This is the last device, the backend will perge the data after this
// An explicit delete account endpoint might be better though
if let thisDevice = devices.first(where: { $0.id == account.deviceId }) {
try await logout(deviceId: thisDevice.id, token: token)
guard statusCode == 204 else {
throw SyncError.unexpectedStatusCode(statusCode)
}
}

Expand Down
5 changes: 4 additions & 1 deletion Sources/DDGSync/internal/Endpoints.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ class Endpoints {
private(set) var baseURL: URL

private(set) var signup: URL
private(set) var connect: URL
private(set) var login: URL
private(set) var logoutDevice: URL
private(set) var connect: URL
private(set) var deleteAccount: URL

private(set) var syncGet: URL
private(set) var syncPatch: URL
Expand All @@ -47,6 +48,7 @@ class Endpoints {
signup = baseURL.appendingPathComponent("sync/signup")
login = baseURL.appendingPathComponent("sync/login")
logoutDevice = baseURL.appendingPathComponent("sync/logout-device")
deleteAccount = baseURL.appendingPathComponent("sync/delete-account")
connect = baseURL.appendingPathComponent("sync/connect")

syncGet = baseURL.appendingPathComponent("sync")
Expand All @@ -63,6 +65,7 @@ extension Endpoints {
signup = baseURL.appendingPathComponent("sync/signup")
login = baseURL.appendingPathComponent("sync/login")
logoutDevice = baseURL.appendingPathComponent("sync/logout-device")
deleteAccount = baseURL.appendingPathComponent("sync/delete-account")
connect = baseURL.appendingPathComponent("sync/connect")

syncGet = baseURL.appendingPathComponent("sync")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ extension RemoteAPIRequestCreating {
func createAuthenticatedJSONRequest(url: URL,
method: HTTPRequestMethod,
authToken: String,
json: Data,
json: Data? = nil,
headers: [String: String] = [:],
parameters: [String: String] = [:]) -> HTTPRequesting {
var headers = headers
Expand Down

0 comments on commit 450f2b4

Please sign in to comment.