feat: add missing crud to provider identity service (#8717)

This commit is contained in:
Christian
2024-08-22 09:39:29 +02:00
committed by GitHub
parent 335061d8cd
commit bb4c58d526
5 changed files with 237 additions and 4 deletions
@@ -8,7 +8,7 @@ export async function createAuthIdentities(
id: "test-id",
provider_identities: [
{
entity_id: "test-id",
entity_id: "provider-test-id",
provider: "manual",
},
],
@@ -20,7 +20,7 @@ export async function createAuthIdentities(
id: "test-id-1",
provider_identities: [
{
entity_id: "test-id-1",
entity_id: "provider-test-id-1",
provider: "manual",
},
],
@@ -28,7 +28,7 @@ export async function createAuthIdentities(
{
provider_identities: [
{
entity_id: "test-id-2",
entity_id: "provider-test-id-2",
provider: "store",
},
],
@@ -335,6 +335,73 @@ moduleIntegrationTestRunner<IAuthModuleService>({
])
})
})
describe("deleteProviderIdentity", () => {
const entity_id = "provider-test-id"
it("should delete the providerIdentities given an id successfully", async () => {
let providerIdentities = await service.listProviderIdentities({
entity_id,
})
expect(providerIdentities).toHaveLength(1)
const providerIdentityId = providerIdentities[0].id
await service.deleteProviderIdentities([providerIdentityId])
providerIdentities = await service.listProviderIdentities({
entity_id,
})
expect(providerIdentities).toHaveLength(0)
})
})
describe("updateProviderIdentity", () => {
const entity_id = "provider-test-id"
it("should throw an error when a id does not exist", async () => {
let error
try {
await service.updateProviderIdentites([
{
id: "does-not-exist",
},
])
} catch (e) {
error = e
}
expect(error.message).toEqual(
'ProviderIdentity with id "does-not-exist" not found'
)
})
it("should update providerIdentity", async () => {
let [providerIdentity] = await service.listProviderIdentities({
entity_id,
})
await service.updateProviderIdentites([
{
id: providerIdentity.id,
provider_metadata: { email: "test@email.com" },
},
])
const providerIdentites = await service.listProviderIdentities({
id: [providerIdentity.id],
})
expect(providerIdentites[0]).toEqual(
expect.objectContaining({
provider_metadata: expect.objectContaining({
email: "test@email.com",
}),
})
)
})
})
})
},
})
@@ -149,6 +149,35 @@ export default class AuthModuleService
>(providerIdentities)
}
updateProviderIdentites(
data: AuthTypes.UpdateProviderIdentityDTO[],
sharedContext?: Context
): Promise<AuthTypes.ProviderIdentityDTO[]>
updateProviderIdentites(
data: AuthTypes.UpdateProviderIdentityDTO,
sharedContext?: Context
): Promise<AuthTypes.ProviderIdentityDTO>
@InjectManager("baseRepository_")
async updateProviderIdentites(
data:
| AuthTypes.UpdateProviderIdentityDTO
| AuthTypes.UpdateProviderIdentityDTO[],
@MedusaContext() sharedContext: Context = {}
): Promise<AuthTypes.ProviderIdentityDTO | AuthTypes.ProviderIdentityDTO[]> {
const updatedProviders = await this.providerIdentityService_.update(
data,
sharedContext
)
const serializedProviders = await this.baseRepository_.serialize<
AuthTypes.ProviderIdentityDTO[]
>(updatedProviders)
return Array.isArray(data) ? serializedProviders : serializedProviders[0]
}
async authenticate(
provider: string,
authenticationData: AuthenticationInput