Skip to content

Commit

Permalink
Merge branch 'padms/1995' into staging
Browse files Browse the repository at this point in the history
  • Loading branch information
padms committed Dec 6, 2023
2 parents 6bc20e1 + 0908d16 commit f5a2c7f
Show file tree
Hide file tree
Showing 10 changed files with 233 additions and 79 deletions.
15 changes: 14 additions & 1 deletion FeatureFlags.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,21 @@ const SATELLITES = [
'poland',
'southkorea',
'storage',
'sheringham',
'dudgeon',
]
const NEWS = [
...GLOBAL_DEV,
...GLOBAL_PROD,
'japan',
'poland',
'brazil',
'germany',
'southkorea',
'celticsea',
'dudgeon',
'sheringham',
]
const NEWS = [...GLOBAL_DEV, ...GLOBAL_PROD, 'japan', 'poland', 'brazil', 'germany', 'southkorea', 'celticsea']
const ARCHIVED_NEWS = [...GLOBAL_PROD, ...GLOBAL_DEV]
const NEWS_SUBSCRIPTION = [...GLOBAL_PROD, ...GLOBAL_DEV]
const MAGAZINE_SUBSCRIPTION = [...GLOBAL_DEV, ...GLOBAL_PROD]
Expand Down
110 changes: 110 additions & 0 deletions sanityv3/scripts/issue-1626/fix-asset-urls.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
//import { type SourceOptions } from 'sanity'
//import { getCliClient } from 'sanity/cli'
//import { sanityClient } from '../../sanity.client'
import { createClient } from '@sanity/client'

const dataset = 'global-development'
const client = createClient({
apiVersion: '2023-08-29',
projectId: process.env.SANITY_STUDIO_API_PROJECT_ID || 'h61q9gi9',
token: process.env.SANITY_STUDIO_MUTATION_TOKEN,
dataset: dataset,
})

const config = client.config()
/*
const client = getCliClient()
const config = client.config()
type ProjectDetails = Pick<SourceOptions, 'projectId' | 'dataset'>
interface AssetDocument {
_id: string
url: string
path: string
}*/

/**
* Ensures the required configuration properties are set.
*/
function isConfigValid(config) {
return typeof config.projectId !== 'undefined' && typeof config.dataset !== 'undefined'
}

/**
* Creates a function that transforms a path string (segments seperated by a `/`
* character), replacing the `${projectId}/${dataset}` segments at the given
* position with the given project details.
*/
function createFixFunction(position) {
return (input, { projectId, dataset }) => {
const input2 = input.split('/').slice() // create a copy of the array
input2.splice(position, 2, projectId, dataset)
return input2.join('/')
//return input.split('/').toSpliced(position, 2, projectId, dataset).join('/')
}
}

/**
* Replaces the `${projectId}/${dataset}` segments in the given path.
*/
const fixPath = createFixFunction(1)

/**
* Replaces the `${projectId}/${dataset}` segments in the given URL.
*/
const fixUrl = createFixFunction(4)

/**
* Queries Sanity dataset for assets that have a `url` or `path` field that
* doesn't match the provided project details.
*/
function fetchBrokenAssets({ projectId, dataset }) {
return client.fetch(
`
*[
_type in ["sanity.imageAsset", "sanity.fileAsset"]
&& (
count(string::split(url, $expectedPath)) != 2
|| count(string::split(path, $expectedPath)) != 2
)
] {
_id,
url,
path
}`,
{
expectedPath: `/${projectId}/${dataset}/`,
},
)
}

/**
* Queries Sanity dataset for assets that have a `url` or `path` field that
* doesn't match the provided project details, and patches them with fields
* that do match.
*/
async function fixAssets(projectDetails) {
const assets = await fetchBrokenAssets(projectDetails)

const transaction = assets.reduce((transaction, asset) => {
return transaction.patch(asset._id, (patch) =>
patch.set({
url: fixUrl(asset.url, projectDetails),
path: fixPath(asset.path, projectDetails),
}),
)
}, client.transaction())

console.log(JSON.stringify(transaction))
const result = await transaction.commit()

console.log(`Fixed ${result.documentIds.length} documents.`)
console.log(result.documentIds)
}

if (!isConfigValid(config)) {
throw new Error('Please specify your project details.')
}

fixAssets(config)
15 changes: 14 additions & 1 deletion satellites.json
Original file line number Diff line number Diff line change
@@ -1 +1,14 @@
["global", "brazil", "germany", "argentina", "poland", "japan", "storage", "equinorfunds", "southkorea", "celticsea"]
[
"global",
"brazil",
"germany",
"argentina",
"poland",
"japan",
"storage",
"equinorfunds",
"southkorea",
"celticsea",
"dudgeon",
"sheringham"
]
10 changes: 10 additions & 0 deletions satellitesConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ const datasets = {
equinorfunds: ['norwegian'],
southkorea: ['english', 'korean'],
celticsea: ['english', 'welsh'],
sheringham: ['english'],
dudgeon: ['english'],
// Test datasets
'global-development': ['english', 'norwegian', 'japanese'],
'global-test': ['english', 'norwegian', 'japanese'],
Expand Down Expand Up @@ -130,6 +132,14 @@ const websiteDomains = {
url: 'https://www.equinorcelticsea.co.uk',
meta: 'equinorcelticsea.co.uk',
},
dudgeon: {
url: 'https://web-dudgeon-equinor-web-sites-preprod.c2.radix.equinor.com/',
meta: 'Dudgeon',
},
sheringham: {
url: 'https://web-sheringham-equinor-web-sites-preprod.c2.radix.equinor.com/',
meta: 'Sheringham Shoal',
},
'global-development': {
url: 'localhost:3000',
meta: 'Equinor',
Expand Down
30 changes: 16 additions & 14 deletions search/IndexSanityContent/events/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,29 @@ export const indexEvents = (language: Language) => (docId: string) => {
const updateAlgolia = flow(indexName, E.map(flow(update, ap(indexSettings))))
const removeIndexFromAlgolia = flow(indexName, E.map(remove))

type RemoveAndMapType = (pages: Event[]) => EventIndex[]
const removeAndMap: RemoveAndMapType = (pages) => {
pages
.filter((page) => page.docToClear)
.map((page) =>
pipe(
removeIndexFromAlgolia(),
E.ap(E.of(page.slug)),
TE.fromEither,
TE.flatten,
T.map(E.fold(console.error, console.log)),
)(),
)
type RemoveAndMapType = (pages: Event[]) => Promise<EventIndex[]>
const removeAndMap: RemoveAndMapType = async (pages) => {
await Promise.all(
pages
.filter((page) => page.docToClear)
.map((page) =>
pipe(
removeIndexFromAlgolia(),
E.ap(E.of(page.slug)),
TE.fromEither,
TE.flatten,
T.map(E.fold(console.error, console.log)),
)(),
),
)
return pipe(pages.map(mapData))
}

return pipe(
getSanityClient(),
TE.fromEither,
TE.chainW(fetchData(language, docId)),
TE.map(removeAndMap),
TE.chainW((pages) => TE.fromTask(() => removeAndMap(pages))),
TE.chainW((data) => pipe(updateAlgolia(), E.ap(E.of(data)), TE.fromEither)),
TE.flatten,
T.map(E.fold(console.error, console.log)),
Expand Down
31 changes: 17 additions & 14 deletions search/IndexSanityContent/localNews/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,30 @@ export const indexLocalNews = (language: Language) => (docId: string) => {
const updateAlgolia = flow(indexName, E.map(flow(update, ap(indexSettings))))
const removeIndexFromAlgolia = flow(indexName, E.map(remove))

type RemoveAndMapType = (pages: LocalNewsArticle[]) => NewsIndex[]
const removeAndMap: RemoveAndMapType = (pages) => {
pages
.filter((page) => page.docToClear)
.map((page) =>
pipe(
removeIndexFromAlgolia(),
E.ap(E.of(page.slug)),
TE.fromEither,
TE.flatten,
T.map(E.fold(console.error, console.log)),
)(),
)
type RemoveAndMapType = (pages: LocalNewsArticle[]) => Promise<NewsIndex[]>
const removeAndMap: RemoveAndMapType = async (pages) => {
await Promise.all(
pages
.filter((page) => page.docToClear)
.map((page) =>
pipe(
removeIndexFromAlgolia(),
E.ap(E.of(page.slug)),
TE.fromEither,
TE.flatten,
T.map(E.fold(console.error, console.log)),
)(),
),
)

return pipe(pages.map(mapData), flatten)
}

return pipe(
getSanityClient(),
TE.fromEither,
TE.chainW(fetchData(language, docId)),
TE.map(removeAndMap),
TE.chainW((pages) => TE.fromTask(() => removeAndMap(pages))),
TE.chainW((data) => pipe(updateAlgolia(), E.ap(E.of(data)), TE.fromEither)),
TE.flatten,
T.map(E.fold(console.error, console.log)),
Expand Down
30 changes: 16 additions & 14 deletions search/IndexSanityContent/magazine/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,28 @@ export const indexMagazine = (language: Language) => (docId: string) => {
const updateAlgolia = flow(indexName, E.map(flow(update, ap(indexSettings))))
const removeIndexFromAlgolia = flow(indexName, E.map(remove))

type RemoveAndMapType = (pages: MagazineArticle[]) => MagazineIndex[]
const removeAndMap: RemoveAndMapType = (pages) => {
pages
.filter((page) => page.docToClear)
.map((page) =>
pipe(
removeIndexFromAlgolia(),
E.ap(E.of(page.slug)),
TE.fromEither,
TE.flatten,
T.map(E.fold(console.error, console.log)),
)(),
)
type RemoveAndMapType = (pages: MagazineArticle[]) => Promise<MagazineIndex[]>
const removeAndMap: RemoveAndMapType = async (pages) => {
await Promise.all(
pages
.filter((page) => page.docToClear)
.map((page) =>
pipe(
removeIndexFromAlgolia(),
E.ap(E.of(page.slug)),
TE.fromEither,
TE.flatten,
T.map(E.fold(console.error, console.log)),
)(),
),
)
return pipe(pages.map(mapData), flatten)
}
return pipe(
getSanityClient(),
TE.fromEither,
TE.chainW(fetchData(language, docId)),
TE.map(removeAndMap),
TE.chainW((pages) => TE.fromTask(() => removeAndMap(pages))),
TE.chainW((data) => pipe(updateAlgolia(), E.ap(E.of(data)), TE.fromEither)),
TE.flatten,
T.map(E.fold(console.error, console.log)),
Expand Down
31 changes: 17 additions & 14 deletions search/IndexSanityContent/news/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,30 @@ export const indexNews = (language: Language) => (docId: string) => {
const updateAlgolia = flow(indexName, E.map(flow(update, ap(indexSettings))))
const removeIndexFromAlgolia = flow(indexName, E.map(remove))

type RemoveAndMapType = (pages: NewsArticle[]) => NewsIndex[]
const removeAndMap: RemoveAndMapType = (pages) => {
pages
.filter((page) => page.docToClear)
.map((page) =>
pipe(
removeIndexFromAlgolia(),
E.ap(E.of(page.slug)),
TE.fromEither,
TE.flatten,
T.map(E.fold(console.error, console.log)),
)(),
)
type RemoveAndMapType = (pages: NewsArticle[]) => Promise<NewsIndex[]>

const removeAndMap: RemoveAndMapType = async (pages) => {
await Promise.all(
pages
.filter((page) => page.docToClear)
.map((page) =>
pipe(
removeIndexFromAlgolia(),
E.ap(E.of(page.slug)),
TE.fromEither,
TE.flatten,
T.map(E.fold(console.error, console.log)),
)(),
),
)
return pipe(pages.map(mapData), flatten)
}

return pipe(
getSanityClient(),
TE.fromEither,
TE.chainW(fetchData(language, docId)),
TE.map(removeAndMap),
TE.chainW((pages) => TE.fromTask(() => removeAndMap(pages))),
TE.chainW((data) => pipe(updateAlgolia(), E.ap(E.of(data)), TE.fromEither)),
TE.flatten,
T.map(E.fold(console.error, console.log)),
Expand Down
32 changes: 18 additions & 14 deletions search/IndexSanityContent/topic/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,31 @@ export const indexTopic = (language: Language) => (docId: string) => {
const updateAlgolia = flow(indexName, E.map(flow(update, ap(indexSettings))))
const removeIndexFromAlgolia = flow(indexName, E.map(remove))

type RemoveAndMapType = (pages: TopicPage[]) => TopicIndex[]
const removeAndMap: RemoveAndMapType = (pages) => {
pages
.filter((page) => page.docToClear)
.map((page) =>
pipe(
removeIndexFromAlgolia(),
E.ap(E.of(page.slug)),
TE.fromEither,
TE.flatten,
T.map(E.fold(console.error, console.log)),
)(),
)
type RemoveAndMapType = (pages: TopicPage[]) => Promise<TopicIndex[]>

const removeAndMap: RemoveAndMapType = async (pages) => {
await Promise.all(
pages
.filter((page) => page.docToClear)
.map((page) =>
pipe(
removeIndexFromAlgolia(),
E.ap(E.of(page.slug)),
TE.fromEither,
TE.flatten,
T.map(E.fold(console.error, console.log)),
)(),
),
)

return pipe(pages.map(mapData), flatten)
}

return pipe(
getSanityClient(),
TE.fromEither,
TE.chainW(fetchData(language, docId)),
TE.map(removeAndMap),
TE.chainW((pages) => TE.fromTask(() => removeAndMap(pages))),
TE.chainW((data) => pipe(updateAlgolia(), E.ap(E.of(data)), TE.fromEither)),
TE.flatten,
T.map(E.fold(console.error, console.log)),
Expand Down
Loading

0 comments on commit f5a2c7f

Please sign in to comment.